前言
本文对shape做个介绍,以及圆角矩形与layer-list
主要元素
gradient – 对应颜色渐变。 startcolor、endcolor就不多说了。 android:angle 是指从哪个角度开始变。
solid – 填充。
stroke – 描边。
corners – 圆角。
padding – 定义内容离边界的距离。与android:padding_left、android:padding_right这些是一个理。
圆角矩形
先抛出结论,圆角矩形的代码如下所示。
<?xml version="1.0" encoding="utf-8"?><!--圆角矩形-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--圆角半径-->
<corners android:radius="15dp" />
<!--填充-->
<solid android:color="@color/white" />
<!--描边-->
<stroke
android:width="1px"
android:color="@color/black"></stroke>
</shape>
这里有个坑,以前我是不写solid的,觉得没必要,后来发现在4.1.2的手机上出来的shape背景是黑色的,我擦,看来solid必须要写的。
layer-list
有时候可以用多个shape叠加来实现想要的效果,用layer-list
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#00ff00" android:width="1px"></stroke>
<solid android:color="#00ffee"></solid>
</shape>
</item>
<item android:bottom="15dp">
<shape android:shape="rectangle">
<solid android:color="#003f77"></solid>
</shape>
</item>
</layer-list>
以上代码就是2个shape的叠加效果如下