日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Android Switch开关按钮使用和自定义样式(系列教程五)

發(fā)布時(shí)間:2024/3/24 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Switch开关按钮使用和自定义样式(系列教程五) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。