說明:大部分內容都是參考別的文章,這裏做整理是爲了以後的編程有實用的模板,可以即需即用。
1、圓
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false">
<solid android:color="#c6c6c6" />
<size
android:width="50dp"
android:height="50dp" />
</shape>
2、帶圓角的長方形
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#b2b2b2" />
<size
android:width="50dp"
android:height="30dp" />
<corners android:radius="5dp" />
</shape>
3、帶圓角的邊框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#ff5a85" />
<size
android:width="50dp"
android:height="30dp" />
<corners android:radius="5dp" />
</shape>
4、狀態切換
// selector_switch_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_open" android:state_checked="true" />
<item android:drawable="@drawable/btn_close" android:state_checked="false" />
</selector>
<ToggleButton
android:layout_width="40dp"
android:layout_height="20dp"
android:background="@drawable/selector_switch_button"
android:textOff="@null"
android:textOn="@null" />
<Switch
android:layout_width="40dp"
android:layout_height="20dp"
android:background="@drawable/selector_switch_button"
android:textOff="@null"
android:textOn="@null" />
5、水平進度條背景
// progress_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 設置progressbar的背景樣式 -->
<item android:id="@android:id/background">
<shape>
<corners android:radius="20dp" />
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#32dcdc" />
</shape>
</item>
<!-- 設置progressbar的進度條樣式 -->
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="20dp" />
<solid android:color="#32dcdc" />
</shape>
</scale>
</item>
</layer-list>
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:max="100"
android:progressDrawable="@drawable/progress_bg" />
6、漸變色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#ff6060"
android:startColor="#ff2434" />
</shape>
<gradient
android:angle="integer" // Integer,代表漸變顏色的角度,必須是45的整數倍,默認是 0,需要 android:type="linear"
android:centerX="integer" // Float,相對X的漸變位置(只有在 type 不爲 linear 的情況下起作用)
android:centerY="integer" // Float,相對Y的漸變位置(只有在 type 不爲 linear 的情況下起作用)
android:centerColor="integer" // Color,顏色漸變的中間顏色,主要用於多彩
android:endColor="color" // Color,顏色漸變的結束顏色
android:startColor="color" // Color顏色漸變的開始顏色
android:gradientRadius="integer" // Float,漸變顏色的半徑,單位應該是像素點. 需要 android:type="radial"
android:type=["linear" | "radial" | "sweep"]
// linear 線性漸變,默認值
// radial 放射性漸變(圓形漸變),起始顏色從 cenralX,centralY點開始
// sweep 掃描式漸變(扇形漸變)
android:useLevel=["true" | "false"] /> // 使用 LevelListDrawable 時就要設置