这个标题不太好取,其实就是实现:在矩形内绘制边线框(stroke),如下图:
一开始拿到这个UI,相信有人跟我一样,立即想到shape、stroke,然后一顿猛虎操作:
在drawable下新建stroke_bg.xml,敲出如下代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="@dimen/x5"
android:color="@color/black"
android:dashWidth="@dimen/x20"
android:dashGap="@dimen/x35" />
<solid android:color="#E6F1FA" />
<corners android:radius="@dimen/x55" />
</shape>
效果图如下,跟UI差一点,怎么使虚线框往里面挪一点距离,添加padding尝试,还是不行!
稍稍思考一会后,那我在上面效果图的基础上,加个阴影不就实现UI了吗?这时候就需要了解一下layer-list(把item效果依次叠加显示)、item,改良后的代码如下(实现了UI):
<?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="#E6F1FA" />
<corners android:radius="@dimen/x55" />
</shape>
</item>
<!--第二层前景,四边的距离是相对第一层的偏移量-->
<item
android:bottom="@dimen/x45"
android:left="@dimen/x45"
android:right="@dimen/x45"
android:top="@dimen/x45">
<shape android:shape="rectangle">
<!-- stroke边框,width宽度,color颜色,
dashWidth虚线的长度(切换大小,看看是长度还是宽度在变化),dashGap虚线间距(为0是实线) -->
<stroke
android:width="@dimen/x2"
android:color="@color/black"
android:dashWidth="@dimen/x15"
android:dashGap="@dimen/x15" />
<!--填充的颜色-->
<solid android:color="#E6F1FA" />
<!--圆角,radius四个角;可以单独设置四个角,
同时设置优先显示"单独设置"的效果,eg:topRightRadius="@dimen/x155",右上角角度是155而不是55-->
<corners android:radius="@dimen/x55" />
</shape>
</item>
</layer-list>