关于android:带有圆角的FrameLayout

FrameLayout with rounded corners

我在根视图(线性:垂直)中有一个FrameLayout,用于根据用户输入保存不同的片段。 FrameLayout小于背景,因此它看起来是"浮动的"。

如何摆放FrameLayout的角落?

FrameLayout内的碎片不是图片,因此无法裁剪。


创建一个xml文件,例如your_rounded_shape.xml,在您的可绘制文件夹中,添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#CCCCCC"/>    

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

    <padding android:left="2dp"
         android:top="2dp"
         android:right="2dp"
         android:bottom="2dp"/>

    <corners android:radius="8dp" />
</shape>

然后将your_rounded_shape.xml用作FrameLayout中的背景。像这样的东西:

1
2
3
4
<FrameLayout
    android:width="match_parent"
    android:height="wrap_content"
    android:background="@drawable/your_rounded_shape"/>


用CardView布局替换FrameLayout。

1
2
3
4
5
6
<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp">

</android.support.v7.widget.CardView>

只是对以上答案的改进:
假设这个xml文件的名称为rounded_frame_layout.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#CCCCCC"/>    

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

    <padding android:left="2dp"
         android:top="2dp"
         android:right="2dp"
         android:bottom="2dp"/>

    <corners android:radius="8dp"/>
</shape>


Then use your drawable as follows:

<FrameLayout
     android:width="match_parent"
     android:height="wrap_content"
     android:background="@drawable/rounded_frame_layout"/>