Summary of the use of selector in Android

Source: Internet
Author: User

The selector in Android is primarily used to change the default background for other nulls, such as the ListView and button controls, which can be used as follows:

1, first in the Res Directory drawable folder, create a new comm_button_bg.xml XML file.

2. Edit the Comm_button_bg.xml file, and its code structure is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"
<!--the background picture when clicked in touch mode--
<Itemandroid:drawable= "@drawable/btn_bg_pressed"android:state_pressed= "true"></Item>
<Itemandroid:drawable= "@drawable/btn_bg_normal"android:state_pressed= "false"></Item> <!--the background picture when you get focus in non-touch mode and click
</selector>

3. You can customize the style you want according to your project needs, as follows:

sets the selection button pressed and not pressed text color changes effect TextColor<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"><!--Set the text color of the control when clicked,---><ItemAndroid:color= "@android: Color/white"android:state_pressed= "true"></Item>    <ItemAndroid:color= "@android: Color/black"android:state_pressed= "false"></Item></selector>

A similar attribute can also be:

Android:state_selected selected

Android:state_focused Get Focus
android:state_pressed Click
Android:state_enabled setting whether to respond to an event, refers to all events

4. Use the Comm_button_bg.xml defined style in the layout file

< Button         Android:background = "@drawable/common_button_bg"     <!----use a custom style----->         android:text= "Test"        android:textcolor= "@drawable/common_button_textcolor_selector"        android:textsize= "20SP"/ >

Summary of the use of selector in Android

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.