1.selector 從單詞的意思來說:選擇器,就是對你的目標的控制。selector主要是用在ListView的item單擊樣式和TextView和Button的點擊樣式。
2.主要屬性介紹:
android:state_selected選中
android:state_focused獲得焦點
android:state_pressed點擊
android:state_enabled設置是否響應事件,指所有事件
3.下面通過例子來說一下selector對TextView設置:
1).在res下創建一個drawable文件夾用於裝textselector.xml ,對點擊樣式的定義放在這里面
2). 將textselector.xml資源加載到TextView上
textselector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#45c01a" android:state_pressed="true"/> <item android:color="#9a9a9a"/> </selector>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:paddingBottom="4dp" android:text="首頁" android:textColor="@drawable/textselector" android:textSize="20sp" > </TextView> </LinearLayout>
運行程序之后要看到顏色改變的效果的話,還必須給TextView添加按鈕監聽事件,就算事件不取執行什么功能都必須去設置。
TextView textView = (TextView) findViewById(R.id.textview); textView.setOnClickListener(null);