Android Switch开关按钮使用和自定义样式(系列教程五)
Switch開關(guān)按鈕簡介
Switch開關(guān)按鈕是Android中的基本控件之一,其本質(zhì)上也是一個(gè)按鈕,具有開和關(guān)兩種展示狀態(tài)。
Switch開關(guān)按鈕基本使用
在布局文件中定義開關(guān)按鈕:
<LinearLayoutandroid:layout_width="300dp"android:layout_height="match_parent"android:layout_centerHorizontal="true"android:layout_marginTop="20dp"android:orientation="vertical"><Switchandroid:id="@+id/swtTest"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>下面是開關(guān)按鈕的默認(rèn)樣式,比較丑,我們后面自定義比較好看的開關(guān)按鈕。?
?在Activity中使用開關(guān)按鈕:
?Switch開關(guān)按鈕本質(zhì)上也是一個(gè)按鈕,也具有對onClick、onLongClick、onTouch事件的處理能力,但它又是一個(gè)特殊的按鈕,擁有一個(gè)特殊的事件,可以監(jiān)聽開關(guān)按鈕的狀態(tài)變化,如下所示:
public class MainActivity05 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main_05);//根據(jù)ID獲取到開關(guān)按鈕Switch swtTest = findViewById(R.id.swtTest);//給開關(guān)按鈕設(shè)置監(jiān)聽狀態(tài)改變事件swtTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {System.out.println("開關(guān)按鈕的狀態(tài) = " + b);}});} }Switch開關(guān)按鈕屬性介紹
- textOn:開關(guān)按鈕打開時(shí)顯示的文字。
- textOff:開關(guān)按鈕關(guān)閉時(shí)顯示的文字。
- thumb:開關(guān)按鈕上原型滑塊的樣式,自定義樣式時(shí)需要設(shè)置此樣式。
- track:開關(guān)按鈕下面導(dǎo)軌的樣式,自定義樣式時(shí)需要設(shè)置此樣式。
- switchTextAppearance:設(shè)置文本的風(fēng)格,可以用來設(shè)置開關(guān)兩種狀態(tài)下的文本樣式。
- checked:設(shè)置初始選中狀態(tài)
- showText:設(shè)置是否顯示開關(guān)上的文字(android系統(tǒng)中默認(rèn)不顯示)
Switch開關(guān)按鈕自定義樣式
自定義樣式效果圖如下,下面我們一步步去實(shí)現(xiàn)這個(gè)樣式。
?1. 定義開關(guān)按鈕底部導(dǎo)軌的樣式:drawable/track.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!--開關(guān)按鈕關(guān)閉時(shí)的導(dǎo)軌樣式--><item android:state_checked="false"><shape><size android:height="@dimen/switchHeight" android:width="@dimen/switchWidth"></size><corners android:radius="20dp"></corners><solid android:color="#b353667c"></solid><stroke android:color="#b3ffffff" android:width="1dp"></stroke></shape></item><!--開關(guān)按鈕打開時(shí)的導(dǎo)軌樣式--><item android:state_checked="true"><shape><size android:height="@dimen/switchHeight" android:width="@dimen/switchWidth"></size><corners android:radius="20dp"></corners><solid android:color="#b32881d5"></solid><stroke android:color="#b3ffffff" android:width="1dp"></stroke></shape></item> </selector>?2. 定義開關(guān)按鈕上滑塊的樣式:drawable/thumb.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="false"><layer-list><item android:width="@dimen/switchHeight" android:height="@dimen/switchHeight"><shape android:shape="oval"> <!-- <solid android:color="#b3536673"></solid>--></shape></item><item android:left="@dimen/thumbOffset" android:right="@dimen/thumbOffset" android:top="@dimen/thumbOffset" android:bottom="@dimen/thumbOffset"><shape android:shape="oval"> <!-- <size android:height="30dp" android:width="30dp"></size>--><solid android:color="#b3536673"></solid><stroke android:width="1dp" android:color="#b3ffffff"></stroke></shape></item></layer-list></item><item android:state_checked="true"><layer-list><item android:width="@dimen/switchHeight" android:height="@dimen/switchHeight"><shape android:shape="oval"><!-- <solid android:color="#b3536673"></solid>--></shape></item><item android:left="@dimen/thumbOffset" android:right="@dimen/thumbOffset" android:top="@dimen/thumbOffset" android:bottom="@dimen/thumbOffset"><shape android:shape="oval"><solid android:color="#b3536673"></solid><stroke android:width="1dp" android:color="#b3ffffff"></stroke></shape></item></layer-list></item> </selector>3. 定義開關(guān)時(shí)的字體樣式:values/style.xml
<style name="switchStyleDefault" parent="@style/TextAppearance.AppCompat.Widget.Switch"><item name="android:textSize">10sp</item><item name="android:textColor">#ffffff</item></style><style name="switchStyleCheck" parent="@style/TextAppearance.AppCompat.Widget.Switch"><item name="android:textSize">10sp</item><item name="android:textColor">#00ff0a</item></style>4. 需要在代碼中根據(jù)按鈕狀態(tài)設(shè)置字體樣式。
//給開關(guān)按鈕設(shè)置監(jiān)聽狀態(tài)改變事件swtTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {System.out.println("開關(guān)按鈕的狀態(tài) = " + b);//設(shè)置改變字體顏色swtTest.setSwitchTextAppearance(MainActivity05.this, b ? R.style.switchStyleCheck : R.style.switchStyleDefault);}});5. 最后,看一下布局中Switch開關(guān)按鈕屬性的設(shè)置,都引用了我們前面定義的樣式。
<Switchandroid:id="@+id/swtAutoModel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/textView5"android:showText="true"android:switchMinWidth="50dp"android:switchTextAppearance="@style/switchStyleDefault"android:textOff="異物"android:textOn="導(dǎo)線"android:thumb="@drawable/thumb"android:track="@drawable/track" />至此,Switch開關(guān)按鈕自定義樣式已經(jīng)實(shí)現(xiàn)。
原創(chuàng)不易,點(diǎn)個(gè)贊再走唄。。。
?
總結(jié)
以上是生活随笔為你收集整理的Android Switch开关按钮使用和自定义样式(系列教程五)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人脸识别论文——发现微小的脸
- 下一篇: 南邮 ctf