Android Beginner Tutorial: How to customize a State selector

Source: Internet
Author: User

There is a scenario where a button or a picture (view) is clicked to change the style (which usually changes the background color). At this point, a solution might be a state selector. Here's how to implement the State selector:

Steps:

First, create a new folder like this: res/drawable

Second, create an XML file; named My_select.xml here (note Selecting a file of select type)

Third, the following code is written in it:

<?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/btn_green_pressed "/> <!--pressed press the color background picture. You can also create a new color file in the values directory to introduce color--        <item android:state_pressed= "false"        android:drawable= "@drawable/btn _green_normal "/> <!--default background picture--></selector>

Note: Those two quoted pictures are their own casually put, they like to change.

Four, in the Layout file to button buttons set to this selector, the code is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <button        android:layout_centerinparent=" true "        android:background=" @drawable/BT _select "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" Dot I change the color Oh "/></relativelayout>

Now run the program on the line, see:

As you can see, the default is the green picture background you set, pressed into a brownish yellow background.

If you set the color picker, the effect is as follows:


However, personal advice not to use color as a selector, for internal reasons, the current knowledge can not be explained. Because some view added color selector will be a problem, I hope to understand the specific principle. Know senior, can also leave a message to explain, in advance thank O (∩_∩) o


Android Beginner Tutorial: How to customize a status selector

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.