歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發 color state list

Android開發 color state list

日期:2017/3/1 11:08:09   编辑:Linux編程

一個ColorStateList是一個對象,定義在xml中,用它作為顏色值來使用。但是否顯示出顏色取決於視圖對象的狀態。例如,一個按鈕有幾個不同的狀態(按下,獲得焦點),可以使用color state list使它在不同的狀態顯示出不同的顏色。

每種顏色定義在<item>元素中,用它的屬性來描述各種狀態,<item>放在唯一的根結點<selecor>中。

語法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
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"] />
</selector>
例子:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/>
<item android:state_focused="true"
android:color="#ff0000ff"/>
<item android:color="#ff000000"/> </selector>
將布局應用到視圖元素中:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />

Copyright © Linux教程網 All Rights Reserved