Background selector selector in Android platform

Source: Internet
Author: User
Tags touch xmlns

 /**
* Add a file in the Res/drawable folder that sets the trigger state of the picture, and you can set the state_pressed,state_checked,state_pressed,
* State_selected,state_focused,state_enabled Status:
*
* android:state_pressed
* If true, display the picture when clicked Displays the default if False is not pressed.
*
* android:state_focused
* True, displayed when focus is received, false, no focus display defaults.
*
* android:state_selected
* True, display the picture when selected, and false to display the picture when it is not selected.
*
* android:state_checkable
* True to display the picture when the checkbox is available; False to display the picture when the checkbox is not available.
*
* android:state_checked
* True to display the picture when the checkbox is selected; False to display the picture when the checkbox is selected.
*
* android:state_enabled
* True to display the picture when it can be used; false to display when the component is not available.
*
* android:state_window_focused
* True to display the picture when the activity has the focus at the front; false to display the picture when it is not in front.
*
*/

A Create an XML file, location: Drawable/xxx.xml, under the same directory remember to put related pictures
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--The default background picture-->
<item android:drawable= "@drawable/pic1"/>
<!--background picture without focus-->
<item android:state_window_focused= "false"
android:drawable= "@drawable/pic1"/>
< background picture--> when you get focus and click in non-touch mode!--
<item android:state_focused= "true" android:state_pressed= "true" android:drawable= "@drawable/pic2"/>
<!--background picture when clicked in touch mode-->
<item android:state_focused= "false" android:state_pressed= "true" android:drawable= "@drawable/pic3"/>
< picture background When selected!---->
<item android:state_selected= "true" android:drawable= "@drawable/pic4"/>
<!--picture background When the focus is obtained-->
<item android:state_focused= "true" android:drawable= "@drawable/pic5"/>
</selector>

Two. Use XML file:
1. Method One: Configure Android:listselector= "@drawable/xxx in ListView
Or add attributes to the ListView item android:background= "@drawable/xxx"
2. Method Two: Drawable drawable = Getresources (). getdrawable (R.DRAWABLE.XXX);
Listview.setselector (drawable); however, this can occur when the list is sometimes black, and you need to add: android:cachecolorhint= "@android: color/transparent" to make it transparent.
Related properties:
Android:state_selected is selected
Android

: State_focused is gaining focus
Android:state_pressed is clicked
Android:state_enabled is setting whether to respond to events, which means all events
The selector effect of the button can also be set based on these states. You can also set selector to change the state of text in the button.

The following are the text effects in the configuration button:
drawable/button_font.xml
<?xml version= "1.0" encoding= "Utf-8"
< Selector xmlns:android= "http://schemas.android.com/apk/res/android"
<item android:state_selected= "true "Android:color=" #FFF/>
<item android:state_focused= "true" android:color= "#FFF"/>
<item Android : State_pressed= "true" android:color= "#FFF"/>
<item android:color= "#000"/>
</selector>
Button can also achieve more complex effects, such as gradient
Drawable/button_color.xml
<?xml version= "1.0" encoding= "Utf-8"
< Selector xmlns:android= "http://schemas.android.com/apk/res/android" >/
<item android:state_pressed= "true ">//defines the form when a button is in a pressed state.
<shape>
<gradient android:startcolor= "#8600ff"/>
<stroke android:width= "2DP" Android: Color= "#000000"/>
<corners android:radius= "5DP"/>
<padding android:left= "10DP" android:top= " 10DP "
android:bottom=" 10DP "android:right=" 10DP "/&Gt
</shape>
</item>
<item android:state_focused= "true" >//define the form when button gets focus
< shape>
<gradient android:startcolor= "#eac100"/>
<stroke android:width= "2DP" android:color= "# 333333 "color=" #ffffff "/>
<corners android:radius=" 8DP "/>
<padding android:left=" 10DP "Android: top= "10DP"
android:bottom= "10DP" android:right= "10DP"/>
</shape>
</item>
</ Selector>
Finally, you need to add two items to the XML file that contains the button. For example Main.xml file, you need to add two items android:focusable= "true" android:background= "@drawable/button_color" in <button/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.