Android应用开发之 android selector下的属性值
在Android 开发,UI设计中为了获得更漂亮的外观,通常都需要改变常用的控件的风格,下面对布局文件xml 中的select属性介绍,比较常用。在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置state_pressed,state_checked,state_pressed,state_selected,state_focused,state_enabled 等几个状态:
android:state_pressed
Boolean. "true" if this item should be used whenthe object is pressed (such as when a button is touched/clicked);"false" if this item should be used in the default, non-pressedstate.
如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。
android:state_focused
Boolean. "true" if this item should be used whenthe object is focused (such as when a button is highlighted using thetrackball/d-pad); "false" if this item should be used in the default,non-focused state.
true,获得焦点时显示;false,没获得焦点显示默认。
android:state_selected
Boolean. "true" if this item should be used whenthe object is selected (such as when a tab is opened); "false" ifthis item should be used when the object is not selected.
true,当被选择时显示该图片;false,当未被选择时显示该图片。
android:state_checkable
Boolean. "true" if this item should be used whenthe object is checkable; "false" if this item should be used when theobject is not checkable. (Only useful if the object can transition between acheckable and non-checkable widget.)
true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。
android:state_checked
Boolean. "true" if this item should be used whenthe object is checked; "false" if it should be used when the objectis un-checked.
true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。
android:state_enabled
Boolean. "true" if this item should be used whenthe object is enabled (capable of receiving touch/click events);"false" if it should be used when the object is disabled.
true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。
android:state_window_focused
Boolean. "true" if this item should be used whenthe application window has focus (the application is in the foreground),"false" if this item should be used when the application window doesnot have focus (for example, if the notification shade is pulled down or adialog appears).
true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed"/><!--pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused"/><!--focused -->
<item android:drawable="@drawable/button_normal"/><!--default -->
</selector>
如在ImageButton 按下效果:
在res/drawable 新建imagebutton_press.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/button2_down" /><!-- press -->
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/button2_over" />
<!--focused -->
<item android:state_enabled="true" android:drawable="@drawable/button2" />
<!-- defauld -->
</selector>
在imagebutton 布局文件中
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/account_button_prsess"/>
就可以体现出imageButtom 按下的效果。