我如何制作圆角布局?我想应用圆角到我的线性布局。


当前回答

对于API 21+,使用剪辑视图

在API 21中,圆形轮廓剪辑被添加到View类中。更多信息请参见培训文档或参考资料。

这个内置的功能使圆角非常容易实现。它适用于任何视图或布局,并支持适当的剪辑。

下面是该怎么做:

创建一个可绘制的圆形形状,并将其设置为视图的背景: android:背景= " @drawable / round_outline” 在代码中剪辑轮廓:setClipToOutline(true)

文档曾经说过,你可以设置android:clipToOutline=“true”的XML,但这个错误现在终于解决了,文档现在正确地指出,你只能在代码中这样做。

它的样子:

关于ImageViews的特别说明

setClipToOutline()仅在视图的背景设置为可绘制的形状时才有效。如果此背景形状存在,视图将背景的轮廓作为剪切和阴影目的的边界。

这意味着如果你想圆角的ImageView与setClipToOutline(),你的图像必须来自android:src而不是android:background(因为背景是用于圆角形状)。如果你必须使用背景来设置你的图像而不是src,你可以使用这个嵌套视图的解决方案:

创建一个外部布局,将其背景设置为可绘制的形状 将布局包装在ImageView周围(没有填充) ImageView(包括布局中的任何其他内容)现在将被剪切到外部布局的圆形形状。

其他回答

如果你想要的只是一个简单的圆角矩形,长话短说。

float r=8;
ShapeDrawable shape = 
   new ShapeDrawable (new RoundRectShape(new float[] { r, r, r, r, r, r, r, r },null,null));
shape.getPaint().setColor(Color.RED);
view.setBackground(shape);

前两个浮动用于左上角(其余对按顺时针方向对应)。

要了解更多细节,请阅读这个答案

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="3dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
    <padding android:left="3dip" android:top="3dip" android:right="3dip" android:bottom="3dip" />
</shape>

@David,只是把padding的值和stroke一样,所以无论图像大小,边框都是可见的

我是这样做的:

检查屏幕截图:

在可绘制文件夹中创建名为custom_rectangle.xml的可绘制文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@android:color/white" />

    <corners android:radius="10dip" />

    <stroke
        android:width="1dp"
        android:color="@android:color/white" />

</shape>

现在在视图上应用矩形背景:

mView.setBackground(R.drawlable.custom_rectangle);

Done

在材质组件库中,你可以使用MaterialShapeDrawable来绘制自定义形状。

只要把线性布局放在你的xml布局:

<LinearLayout
    android:id="@+id/linear_rounded"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ..>

    <!-- content ..... -->

</LinearLayout>

然后在你的代码中你可以应用一个shapeappearance emodel。喜欢的东西:

float radius = getResources().getDimension(R.dimen.default_corner_radius);

LinearLayout linearLayout= findViewById(R.id.linear_rounded);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,radius)
    .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
//Fill the LinearLayout with your color
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.secondaryLightColor));


ViewCompat.setBackground(linearLayout,shapeDrawable);

注意:它需要材质组件库的1.1.0版本。

如果你想让你的布局圆润,最好使用CardView,它提供了许多功能,使设计美观。

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="5dp">
      <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:text="@string/quote_code"
                android:textColor="@color/white"
                android:textSize="@dimen/text_head_size" />
      </LinearLayout>
</android.support.v7.widget.CardView>

使用这个card_view:cardCornerRadius="5dp",你可以改变半径。