如何在Android的相对布局或其他布局中正确设置圆角?

15

安卓让我崩溃了。

我想为相对布局设置圆角,这很简单。

可用的 Drawable XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
 <padding android:left="1dp" android:top="1dp"
            android:right="2dp" android:bottom="2dp" />
    <solid android:color="#FFFFFF" />

    <corners android:radius="15dp" />

    <stroke
        android:width="2dp"
        android:color="#FFF" />

</shape>

但是,如果我将其他布局或者像ImageView这样的小部件插入到相对布局中,我就会得到没有被裁剪的子元素 - 可以看到图片。

enter image description here

你可以看到我得到了未被裁剪的子元素。

我该怎么做才能正确实现我的需求呢?

也许这会有所帮助,我想让圆角效果像iOS一样。

givenView.layer.cornerRadius =roundAngle;
    [givenView.layer setBorderColor:[[borderColor colorWithAlphaComponent:alphaBorder] CGColor]];
    [givenView.layer setBorderWidth:borderWidth];
    givenView.clipsToBounds = YES;
此外,我想向所有认为填充是解决方案的人展示:
enter image description here
这里设置了填充。
为了正确理解问题,请查看层次结构图:
enter image description here
在这里:
RelativeLayout - 在xml中描述了背景
LinearLayout - 包含具有复杂布局的自定义对象的容器,其中包含图像作为背景和其他小部件
profileStatistic - 具有许多子对象的复杂自定义小部件在外部布局中。
完整的布局图片如下所示:
enter image description here
以及内部控件(profileStatistic)的布局:
enter image description here
附言:我不需要9-path。问题与9-path无关!
1个回答

1

如果您看到圆角图像被应用了,但布局的子元素没有圆角矩形。这就是为什么该角落会超出父元素的背景。要解决这个问题,您可以给父布局添加一些填充,以便它不会超出父布局的背景。 或者,您也可以将相同类型的圆角形状可绘制对象应用于子视图。


这不正确。因为这里的情况是:如果圆角设置为15dp,并且填充(用于校正)设置为5dp,那么我会在框架边框和内部内容小部件之间得到空间位置。 - Dmitry Nelepov
给定的5dp填充对于15dp半径不足够,这就是为什么那些角落会超出边界。您可以增加填充并进行检查。 - Raj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接