Android selector 使用注意.
下面是幾個是實現的效果
效果圖demo 地址
selector 的作用:
selector 是選擇,用來修改修改控件的背景,設置點擊效果,等, 自己一般使用在點擊之后改變控件的背景色以及文字的顏色,比如底部導航欄切換等, 對單個控件按下一個狀態,松開之后有一個狀態,就是要實現按壓效果,別的沒怎么用得到,
selector 的屬性如下
android:state_pressed=["true" | "false"]? --是否觸摸
android:state_focused=["true" | "false"]? --是否獲得焦點
android:state_selected=["true" | "false"]? --是否被狀態
android:state_checkable=["true" | "false"]? --是否可選
android:state_checked=["true" | "false"]? --是否選中
android:state_enabled=["true" | "false"]? --是否可用
android:state_window_focused=["true" | "false"]? --是否窗口聚焦
以上屬性,如果對于單個控件,比如,一個Button ,一個Image ,一個Text 或者是一個布局, 使用的屬性為: android:state_pressed 其他seleced 或者 checked 屬性即使設置了也是沒有效果的,
所以使用的時候要注意下選擇的屬性是否對,一個Image ,一個Text 或者是一個布局使用 android:state_pressed 點擊之后沒有效果,可以在xml 中對控制設置android:clickable="true" 這樣就有效果
android:state_selected=["true" | "false"]? --是否被狀態? 這個是針對list 的item 是否被選中的,
android:state_checked=["true" | "false"]? --是否選中 設計多選的可以使用,比如 CheckBox和RadioButton 等,上面提到導航欄狀態切換就是這個屬性,
下面是一個 RadioButton 切換的代碼
<RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/ra_selector_bg"android:textSize="20sp"android:text="單選"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/mult_selector_bg"android:textSize="20sp"android:text="雙選" /></RadioGroup>
ra_selector_bg
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/single_select" android:state_checked="true" /><item android:drawable="@drawable/single_unselect" /></selector>
?
mult_selector_bg
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/mult_select" android:state_checked="true" /><item android:drawable="@drawable/mult_unselect" /></selector>
?
總結
以上是生活随笔為你收集整理的Android selector 使用注意.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 喝白葡萄酒有哪些好处?红葡萄酒和白葡萄酒
- 下一篇: ImageView / Text 使用