Android selector 使用注意.
下面是幾個(gè)是實(shí)現(xiàn)的效果
效果圖demo 地址
selector 的作用:
selector 是選擇,用來修改修改控件的背景,設(shè)置點(diǎn)擊效果,等, 自己一般使用在點(diǎn)擊之后改變控件的背景色以及文字的顏色,比如底部導(dǎo)航欄切換等, 對(duì)單個(gè)控件按下一個(gè)狀態(tài),松開之后有一個(gè)狀態(tài),就是要實(shí)現(xiàn)按壓效果,別的沒怎么用得到,
selector 的屬性如下
android:state_pressed=["true" | "false"]? --是否觸摸
android:state_focused=["true" | "false"]? --是否獲得焦點(diǎn)
android:state_selected=["true" | "false"]? --是否被狀態(tài)
android:state_checkable=["true" | "false"]? --是否可選
android:state_checked=["true" | "false"]? --是否選中
android:state_enabled=["true" | "false"]? --是否可用
android:state_window_focused=["true" | "false"]? --是否窗口聚焦
以上屬性,如果對(duì)于單個(gè)控件,比如,一個(gè)Button ,一個(gè)Image ,一個(gè)Text 或者是一個(gè)布局, 使用的屬性為: android:state_pressed 其他seleced 或者 checked 屬性即使設(shè)置了也是沒有效果的,
所以使用的時(shí)候要注意下選擇的屬性是否對(duì),一個(gè)Image ,一個(gè)Text 或者是一個(gè)布局使用 android:state_pressed 點(diǎn)擊之后沒有效果,可以在xml 中對(duì)控制設(shè)置android:clickable="true" 這樣就有效果
android:state_selected=["true" | "false"]? --是否被狀態(tài)? 這個(gè)是針對(duì)list 的item 是否被選中的,
android:state_checked=["true" | "false"]? --是否選中 設(shè)計(jì)多選的可以使用,比如 CheckBox和RadioButton 等,上面提到導(dǎo)航欄狀態(tài)切換就是這個(gè)屬性,
下面是一個(gè) 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>
?
總結(jié)
以上是生活随笔為你收集整理的Android selector 使用注意.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 喝白葡萄酒有哪些好处?红葡萄酒和白葡萄酒
- 下一篇: ImageView / Text 使用