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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Selector的简写

發(fā)布時(shí)間:2025/3/8 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Selector的简写 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在Android中,我們需要實(shí)現(xiàn)一個(gè)View的點(diǎn)擊或選中樣式的改變,大多數(shù)情況下我們都是使用drawable文件,也就是selector,shape等組成的資源文件。大致套路如下的常規(guī)寫法:

常規(guī)寫法

比如新建一個(gè)正常狀態(tài)下的shape文件sp_normal.xml,選中狀態(tài)的sp_checked.xml,組合兩個(gè)shape的sr_default.xml。

<?xml version="1.0" encoding="utf-8"?> <!--sp_normal.xml--> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@android:color/black" /><corners android:radius="4dp" /> </shape> 復(fù)制代碼<?xml version="1.0" encoding="utf-8"?> <!--sp_checked.xml--> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@android:color/holo_red_light" /><corners android:radius="4dp" /> </shape> 復(fù)制代碼<?xml version="1.0" encoding="utf-8"?> <!--sr_default.xml--> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/sp_normal" android:state_checked="false" /><item android:drawable="@drawable/sp_checked" android:state_checked="true" /> </selector> 復(fù)制代碼

然后將sr_default.xml設(shè)置給View的background

<CheckedTextViewandroid:id="@+id/tv_normal_n"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="44dp"android:background="@drawable/sr_default"android:checked="false"android:padding="10dp"android:text="普通的 checked == false"android:textColor="@android:color/white"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><CheckedTextViewandroid:id="@+id/tv_normal_c"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="24dp"android:background="@drawable/sr_default"android:checked="true"android:padding="10dp"android:text="普通的 checked == true"android:textColor="@android:color/white"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@id/tv_normal_n" /> 復(fù)制代碼

顯示效果如下:

下面我們來看看怎么簡寫這個(gè)樣式。

簡易寫法

我們只需要新建一個(gè)sr_simplify.xml文件,父標(biāo)簽是selector,子shape標(biāo)簽用item標(biāo)簽包裹。

<?xml version="1.0" encoding="utf-8"?> <!--sr_simplify.xml--> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="false"><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@android:color/black" /><corners android:radius="4dp" /></shape></item><item android:state_checked="true"><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@android:color/holo_red_light" /><corners android:radius="4dp" /></shape></item></selector> 復(fù)制代碼

布局跟上面布局差不多,直接看顯示效果:

總結(jié)

我們實(shí)踐發(fā)現(xiàn)兩種寫法都可以實(shí)現(xiàn)同樣的需求,但是具體要使用哪種可以依據(jù)自己的實(shí)際情況來抉擇。

  • 方式一可以實(shí)現(xiàn)更好的解耦,shape文件也可以復(fù)用
  • 方式二寫起來更加簡單,也減少了樣式文件

源碼地址:https://github.com/simplepeng/AndroidExamples/tree/master/example_simplify_selector

總結(jié)

以上是生活随笔為你收集整理的Android Selector的简写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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