Android Control selector Selector

Source: Internet
Author: User

Take button as an example:

General Button has 2 states, pressed and normal state

1. Create a new drawable folder under Res to store your own drawable files

2. Create a new pressed and normal shape file under the Drawable folder (which is a picture resource, skip this step)

3. New Button Background Selector file

4. Specify drawable for the backgroud of the button

Code:

Button_noraml.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >

<stroke
Android:width= "1DP"
Android:color= "#00FF00"/>

<gradient
Android:angle= "270"
Android:endcolor= "#e5e5e5"
Android:startcolor= "#DCDCDC"/>

</shape>

————————————————————————————————————————————————————

Button_press.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >

<!--strokes--
<stroke
Android:width= "1DP"
Android:color= "#00FF00"/>
<!--gradients--
<gradient
Android:angle= "270"
Android:endcolor= "#00FF00"
Android:startcolor= "#90EE90"/>

</shape>

————————————————————————————————————————————————————

Button_selector.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >

<!--pressed state drawable--
<item android:drawable= "@drawable/button_press" android:state_pressed= "true" ></item>
<!--normal state of drawable---
<item android:drawable= "@drawable/button_noraml" ></item>

</selector>

————————————————————————————————————————————————————

<?xml version= "1.0" encoding= "Utf-8"?
<linearlayout xmlns:android= "http://schemas.android.com/apk /res/android "
    android:layout_width=" match_parent "
    android:layout_ height= "Match_parent"
    android:orientation= "vertical";

    < Button
        android:layout_width= "match_parent"
         android:layout_height= "Wrap_content"
        Android:layout_margin= "30DP"
        android:background= "@drawable/button_selector"
        android:text= "button"/>

</LINEARLAYOUT>

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.