Selector-背景选择器
生活随笔
收集整理的這篇文章主要介紹了
Selector-背景选择器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Selector-背景選擇器
1.主要屬性
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 控件可用時 --><item android:drawable="@drawable/enable" android:state_enabled="false"/><!-- 當前窗口失去焦點時 --><item android:drawable="@drawable/lost_window_focused" android:state_window_focused="false"/><!-- 被激活時 --><item android:drawable="@drawable/activated" android:state_activated="true" /><!--獲得焦點時--><item android:drawable="@drawable/focused" android:state_focused="true"/><!--被按壓時--><item android:drawable="@drawable/pressed" android:state_pressed="true"/><!--被選中時--><item android:drawable="@drawable/selected" android:state_selected="true"/><!--默認狀態--><item android:drawable="@drawable/default"/> </selector>使用方式: android:src="@drawable/bg_test"
注意:默認狀態需要寫在最后。
因為Item是從上往下匹配的,如果匹配到合適的,就不會再往下匹配了,
就是說如果默認狀態寫在最上方,則只會匹配默認狀態,不會往下走。
2.另外除了可以設置背景圖片外,還可以設置文本顏色
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!--選中狀態時--><item android:color="@android:color/holo_blue_light" android:state_selected="true"/><!--默認狀態--><item android:color="@android:color/black"/> </selector>使用方式: android:textColor="@drawable/text_color"
另外有一點需要注意注意的是,當你想給TextView,ImageView或者Button設置選中時背景圖片,需要在控件被點擊后,動態設置 btn.setSelected(true); 切記,切記。
轉載于:https://www.cnblogs.com/-Tiger/p/7600635.html
總結
以上是生活随笔為你收集整理的Selector-背景选择器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: storyboard或者Xib给View
- 下一篇: Python-解决粘包