在drawable里面定义xml,在background中引用
<Button
android:id="@+id/completeBtn"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginTop="32dp"
android:text="完成"
android:textColor="@color/white"
android:textSize="18sp"
android:background="@drawable/green_radius"/>
1. 圆角矩形
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#10B703" />
<!-- 应该是圆角 -->
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
2. 左右边框为半圆的矩形
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#10B703" />
<!-- 应该是控件高度的一半 -->
<corners android:radius="25dp" />
</shape>
</item>
</layer-list>
3. 带边框的圆角矩形
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<!-- 边框 -->
<stroke android:color="@color/grey97" android:width="1px"/>
<!-- 主背景色 -->
<solid android:color="@color/white" />
<!-- 圆角 -->
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
4. 只有部分边角是圆角、别的是直角的矩形
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke android:color="@color/grey97" android:width="1px"/>
<solid android:color="@color/white" />
<!-- 需要圆角的地方设置大于0的数值 -->
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="0dp"/>
</shape>
</item>
</layer-list>
refer Creating a rectangle shape with only two rounded edges
5. 某个边框不显示的矩形
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 不需要的边框标识为边框的width负值 -->
<item android:left="-1px">
<shape>
<stroke android:color="@color/grey97" android:width="1px"/>
<solid android:color="#F0F0F0" />
<!-- 圆角控制 -->
<corners
android:bottomRightRadius="4dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="4dp"/>
</shape>
</item>
</layer-list>
refer Open-sided Android stroke
6. 圆形
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!-- 由控件定义的地方保证高宽相等 -->
<shape
android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<stroke android:color="#DCDCDC" android:width="1dp"/>
</shape>
</item>
</layer-list>
7. 点击变换背景
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 正常无点击的情况 -->
<item android:state_pressed="false">
<shape>
<!-- 可以添加stroke等其他特征 -->
<solid android:color="@color/color1" />
<corners android:radius="25dp" />
</shape>
</item>
<!-- 点击时的情况 -->
<item android:state_pressed="true">
<shape>
<solid android:color="@color/color2" />
<corners android:radius="25dp" />
</shape>
</item>
</selector>
8. ProgressBar颜色渐变,在progressDrawable中引用
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 初始背景 -->
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#ff9d9e9d"
android:endColor="#ff9d9e9d"
/>
</shape>
</item>
<!-- 过程中的渐变 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="@color/light_orange"
android:endColor="@color/main_orange" />
</shape>
</clip>
</item>
</layer-list>
refer How to Customize a Progress Bar In Android
Custom Drawable for ProgressBar/ProgressDialog
9. 普通的线性渐变
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="0"
android:startColor="#C8C8C8"
android:centerColor="#979797"
android:endColor="#C8C8C8"
android:type="linear" />
</shape>
</item>
</layer-list>
10. ProgressBar或者SeekBar中间进度条嵌在内部
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/dirtyWhite"/>
</shape>
</item>
<item android:id="@android:id/progress"
android:top="1dp"
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
</scale>
</item>
</layer-list>