Android instance-Mobile security Defender (17)-Custom button background style

Source: Internet
Author: User

First, the goal.

Buttons (button) default, pressed, get focus, and so on, its background displays a custom picture.

Second, the code implementation.

1, create a new drawable folder under the Res folder, create a new file under the new Drawable folder (right-click-new-file), named Button.xml.

2. In the new file (Button.xml)

①. Specifies that the XML version is 1.0 and the encoding format is utf-8 (that is, the first behavior: <?xml version= "1.0" encoding= "Utf-8"?>).

②. New <selector> </selector> tag, specify the namespace as xmlns:android= "Http://schemas.android.com/apk/res/android" in its properties.

③. Add <item> tags in the new <selector> </selector> tags, and in their properties, develop various states and picture resources displayed in that state. If the property is state_pressed True, the picture displayed is @drawable/btn_default_pressed. When the property value has only android:drawable values, this is the picture that is displayed by default.

Code for the Button.xml file:

1<?xml version= "1.0" encoding= "Utf-8"?>2<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >3 4<item android:state_pressed= "true"5android:drawable= "@drawable/btn_default_pressed" ></item> <!--Press---6     7<item android:state_enabled= "true"8android:drawable= "@drawable/btn_default_selected"/> <!--focused--9 Ten<item android:drawable= "@drawable/btn_default_normal_disable"/> <!--default- One  A</selector>
View Code

3. Refer to the style resource file in the Background property in the button buttons that need to use the background style.

button component Code;

1   <button2             android:layout_width= "Wrap_content"3             android:layout_height= " Wrap_content "4             android:layout_alignparentbottom=" true "5             Android:layout_ Alignparentright= "true"6             android:layout_marginbottom= "5dip"7             android:layout_ marginright= "5dip"8             android:background= "@drawable/button"9             android:text= "Next"/ >
View Code

Three, the above button background style implementation can refer to the source code.

In the Styles.xml file in the Android-19\data\res\values folder, find the <style> tag for name= "Widget.button", The item tag of its background is the style of the specified Android:drawable/btn_default, under the Drawable folder Btn_ The code in the Default.xml file is the background style that specifies the button, which can be changed by customizing the button background style.

Code for Widget.button tags in styles.xml:

 1  <style name= "Widget.button" >2  <item name= "Android:background" > @android:d rawable/btn_default</item>3  <item name= "android:focusable" >true  </item>4  <item name= "android:clickable" >true  </item>5  <item name= "Android:textappearance" >?android:attr/textappearancesmallinverse</item>6  <item name= "Android:textcolor" > @android: Color/primary_text_light</item>7  <item name= "android:gravity" >center_vertical|center_horizontal</item>8  </style> 
View Code

btn_default.xml Code:

1<?xml version= "1.0" encoding= "Utf-8"?>2 3<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >4<item android:state_window_focused= "false" android:state_enabled= "true"5android:drawable= "@drawable/btn_default_normal"/>6<item android:state_window_focused= "false" android:state_enabled= "false"7android:drawable= "@drawable/btn_default_normal_disable"/>8<item android:state_pressed= "true"9android:drawable= "@drawable/btn_default_pressed"/>Ten<item android:state_focused= "true" android:state_enabled= "true" Oneandroid:drawable= "@drawable/btn_default_selected"/> A<item android:state_enabled= "true" -android:drawable= "@drawable/btn_default_normal"/> -<item android:state_focused= "true" theandroid:drawable= "@drawable/btn_default_normal_disable_focused"/> -<Item -android:drawable= "@drawable/btn_default_normal_disable"/> -</selector>
View Code

<style name= "Widget.button" > <item name= "android:background" > @android:d rawable/btn_default</item&        Gt        <item name= "android:focusable" >true</item> <item name= "android:clickable" >true</item> <item name= "android:textappearance" >?android:attr/textAppearanceSmallInverse</item> <item name= "an Droid:textcolor "> @android:color/primary_text_light</item> <item name=" Android:gravity ">center_ Vertical|center_horizontal</item> </style>

Android instance-Mobile security Defender (17)-Custom button background style

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.