Demo
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/btn"
android:layout_centerInParent="true"
android:background="@drawable/btn_bg"
android:textSize="16sp"
android:textColor="#fff"
android:text="點擊" />
</RelativeLayout>
res/drawable/btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/rectangle1"
android:state_pressed="false"></item>
<item
android:drawable="@drawable/rectangle2"
android:state_pressed="true"></item>
</selector>
res/drawable/rectangle1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="10dp"></corners>
<solid
android:color="#07b226"></solid>
</shape>
res/drawable/rectangle2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="10dp"></corners>
<solid
android:color="#de3f3f"></solid>
</shape>
運行
StateList資源
文件位置
res/drawable/filename.xml
資源引用
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_hovered=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
標籤&標籤屬性
<selector>
這必須是根元素。包含一個或多個 元素。
屬性
android:constantSize
布爾值。如果可繪製對象報告的內部大小在狀態變更時保持不變,則值爲“true”(大小是所有狀態的最大值);如果大小根據當前狀態而變化,則值爲“false”。默認值爲 false。
android:dither
布爾值。值爲“true”時,將在位圖的像素配置與屏幕不同時(例如:ARGB 8888 位圖和 RGB 565 屏幕)啓用位圖的抖動;值爲“false”時則停用抖動。默認值爲 true。
android:variablePadding
布爾值。如果可繪製對象的內邊距應根據選擇的當前狀態而變化,則值爲“true”;如果內邊距應保持不變(基於所有狀態的最大內邊距),則值爲“false”。啓用此功能要求您在狀態變更時處理執行佈局,這通常不受支持。默認值爲 false。
<item>
定義要在某些狀態期間使用的可繪製對象,如其屬性所述。必須是 <selector> 元素的子項。
屬性
android:drawable
可繪製對象資源。引用可繪製對象資源。
android:state_pressed
布爾值。如果在按下對象(例如觸摸/點按某按鈕)時應使用此項目,則值爲“true”;如果在默認的未按下狀態時應使用此項目,則值爲“false”。
android:state_focused
布爾值。如果在對象具有輸入焦點(例如當用戶選擇文本輸入時)時應使用此項目,則值爲“true”;如果在默認的非焦點狀態時應使用此項目,則值爲“false”。
android:state_hovered
布爾值。如果當光標懸停在對象上時應使用此項目,則值爲“true”;如果在默認的非懸停狀態時應使用此項目,則值爲“false”。通常,這個可繪製對象可能與用於“聚焦”狀態的可繪製對象相同。
此項爲 API 級別 14 新引入的配置。
android:state_selected
布爾值。如果在使用定向控件瀏覽(例如使用方向鍵瀏覽列表)的情況下對象爲當前用戶選擇時應使用此項目,則值爲“true”;如果在未選擇對象時應使用此項目,則值爲“false”。
當焦點 (android:state_focused) 不充分(例如,列表視圖有焦點但使用方向鍵選擇其中的項目)時,使用所選狀態。
android:state_checkable
布爾值。如果當對象可選中時應使用此項目,則值爲“true”;如果當對象不可選中時應使用此項目,則值爲“false”。(僅當對象可在可選中與不可選中小部件之間轉換時纔有用。)
android:state_enabled
布爾值。如果在對象啓用(能夠接收觸摸/點擊事件)時應使用此項目,則值爲“true”;如果在對象停用時應使用此項目,則值爲“false”。
android:state_activated
布爾值。如果在對象激活作爲持續選擇(例如,在持續導航視圖中“突出顯示”之前選中的列表項)時應使用此項目,則值爲“true”;如果在對象未激活時應使用此項目,則值爲“false”。
此項爲 API 級別 11 新引入的配置。
android:state_window_focused
布爾值。如果當應用窗口有焦點(應用在前臺)時應使用此項目,則值爲“true”;如果當應用窗口沒有焦點(例如,通知欄下拉或對話框出現)時應使用此項目,則值爲“false”。
(注:請記住,Android 將應用狀態列表中第一個與對象當前狀態匹配的項目。因此,如果列表中的第一個項目不含上述任何狀態屬性,則每次都會應用它,這就是默認值應始終放在最後的原因)