Android uses selector custom buttons

Source: Internet
Author: User

Visual editor, alt+1 to 9 keys, switch to Tool area, TAB key, select button, ESC, return to edit
Android uses selector custom buttons

When you previously defined a button, you would always use a custom button to get it in the activity and then rewrite the button's Ontouchlistener and then toggle the background image of the button. Not only does this make the code in the activity bloated, but the button cannot be reused.



Later came into contact with the Android selector very useful, the following details how to use selector, first create an XML file under the Res\drawable folder

650) this.width=650; "Src=" http://img.blog.csdn.net/20141020204242187?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmtc4mjuyoa==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Center "/>

Then add the image you want to apply to the button in the drawable file, including the pressed picture and the not-on-time picture.

Then add it in the file: drawable

<?xml version= "1.0"  encoding= "Utf-8"? ><selector xmlns:android= "http// Schemas.android.com/apk/res/android " >     <!--defines the picture to display when the button gets focus-->      <item                    android:state_focused =  "true"                     android:drawable =   "@drawable/image_btn_down"/>        <!--define a realistic picture when the button is selected  -->        <item                    android:state_selected =  " True "                    android:drawable =  "@drawable/image_btn_down "/>    <!--define the displayed picture when the button is clicked-->             <item                    android:state_pressed =  "true"                      android:drawable =  "@drawable/image_btn_down"/>     <!-- The default button is not selected when the picture is displayed-->                <item android:drawable =  "@drawable/image_btn_up"  />   </ Selector>

The file name is called Button_selector.xml, after the completion of the button you want to apply the Drawable property to @drawable/button_selector can be as follows:

<Button                         android:id= "@+id/button_add"                           android:layout_width= "115DP"                          android:layout_height= "Wrap_content"                          android:layout_margin= "5px"                          android: layout_weight= "1"                          android:background= "@drawable/button_selector"                          android: text= "Customize Button"  />






Development Micro-Blog recommendation: http://crh.chinacloudsites.cn/



This article is from the "developer" blog, so be sure to keep this source http://tang1513.blog.51cto.com/7678175/1566330

Android uses selector custom buttons

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.