——各类控件的使用(控件)-开关 【若对该知识点有更多想了解的,欢迎私信博主~~】
按钮(此文件名thumb)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--高度40--> <size android:width="40dp" android:height="40dp" /> <!--圆角弧度20--> <corners android:radius="20dp" /> <!--变化率--> <gradient android:endColor="#eeeeee" android:startColor="#eeeeee" /> <stroke android:width="1dp" android:color="#33da33" /> </shape> </item> <item android:state_checked="false"> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--高度40--> <size android:height="40dp" android:width="40dp" /> <!--圆角弧度20--> <corners android:radius="20dp"/> <!--变化率--> <gradient android:endColor="#eeeeee" android:startColor="#eeeeee" /> <stroke android:width="1dp" android:color="#666666" /> </shape> </item> </selector>轨道(此文件名track)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--高度30此处设置宽度无效--> <size android:height="40dp"/> <!--圆角弧度15--> <corners android:radius="15dp"/> <!--变化率--> <gradient android:endColor="#33da33" android:startColor="#33da33"/> </shape> </item> <item android:state_checked="false"> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--高度30此处设置宽度无效--> <size android:height="40dp"/> <!--圆角弧度15--> <corners android:radius="15dp"/> <!--变化率定义从左到右的颜色不变--> <gradient android:endColor="#888888" android:startColor="#888888"/> </shape> </item> </selector>使用(XML样式文件中)
<Switch android:id="@+id/switch1" android:layout_width="110dp" android:layout_height="160dp" android:checked="true" android:showText="true" android:splitTrack="true" android:switchMinWidth="110dp" android:textOff="off" android:textOn="on" android:thumb="@drawable/thumb" android:track="@drawable/track" />