如何创建具有内圆角的矩形形状

时间:2015-06-24 08:05:46

标签: android

enter image description here

我想创建Drawable,如上图所示的白色形状。

我使用以下代码

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

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

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

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

<size
    android:height="150dp"
    android:width="150dp" /></shape>

但是我得到像这样的输出..

enter image description here

我还尝试了layer-list多个Shapes,但无法获得所需的输出..

我的ImageView的高度和宽度也是按代码计算的。所以我也不想在drawable中传递大小。有可能??

1 个答案:

答案 0 :(得分:1)

您可以使用以下xml绘制形状;

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#fff" ></solid>

            <size
                android:width="100dp"
                android:height="100dp"/>
        </shape>
    </item>
    <item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
        <shape android:shape="rectangle">
            <corners android:radius="10dp" />
            <solid android:color="#000" ></solid>
            <size
                android:width="90dp"
                android:height="90dp"/>
        </shape>
    </item>

</layer-list>

我们互相画两件物品。在第一层上放置一个白色正方形,并在白色正方形内放置另一个半径为10dp,边距为5dp的黑色正方形。

您还可以控制可绘制分配的imageView的宽度和高度,并相应地缩放。

small version

large version 200dp

相关问题