We have noticed that the buttons in some applications are not the same as the normal state when they are clicked, such as:
In normal state: In the selected state:
How is this effect achieved? In the Android system provides us with a convenient and to implement this function, namely: State list drawable.
statelistdrawable is a drawable object defined in XML, we can display different states by setting a picture under different item, depending on the state of the Drawable object. For example, a button control can have different states (click, Focus, and so on), and with a series of drawable objects, you can provide a different background image for each different state.
This sequence of states can be described by an XML file that <selector>
<item>
represents each background with elements under one element, each of which <item>
can describe a state using different attribute values.
When the state changes, the status list is scanned from top to bottom, and the first picture that matches the current state is used. This option is not based on best bets, but simply selects the first item that meets the criteria.
Its XML file is defined as follows:
<?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=["t Rue "| "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>
1. Create your own. xml files, such as Button_selector.xml, in the/res/drawable directory.
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_enabled=" true "android:state_window_focused=" false "android:drawable=" @ Drawable/button_background "/> <item android:state_pressed=" true "android:state_window_focused=" true " android:drawable= "@drawable/button_background_selected"/> <item android:state_focused= "true" android:drawable= "@drawable/button_background"/> <item android:drawable= "@drawable/button_background" /></selector>
2. Reference in the layout XML file:
<button android:id= "@+id/bt_first_dialog_confirm" android:layout_width= "140dip" android:layout_ height= "40dip" android:background= "@drawable/button_selector" android:text= "OK" android:textcolor= "#ffffffff"/>
We can also set the effect of the GridView neutron control when clicked. We will not repeat it here.
"Side do project side learn Android" mobile Security defender 08-some layout and display details: State List