Android button background

Source: Internet
Author: User

Here I will give an example of how to use selector and layer-list to complete the cascading combination of the button background, so as to have different effects in different situations.

Selector in button_ctrl.xml

 <?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/button_highlight_ctrl" />
<item android:drawable="@drawable/button_normal_ctrl" />
</selector>

Button_highlight_ctrl.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/button_highlight_back"
android:top="2dip"
android:right="2dip"
android:bottom="2dip"
android:left="2dip"/>
<item
android:drawable="@drawable/button_highlight_fore"
android:top="2dip"
android:right="2dip"
android:bottom="2dip"
android:left="2dip"/>
</layer-list>

The background color gradient is completed in button_highlight_back.xml.

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dip"/>
<gradient
android:type="linear"
android:angle="270"
android:startColor="@color/highlight_button_start_color"
android:centerColor="@color/highlight_button_middle_color"
android:endColor="@color/highlight_button_end_color"/>
</shape>

Button_highlight_fore is a foreground image, that is, the top shadow when the image is pressed:

So far, the preparation of the highlighted button has been completed, followed by the normal button

Button_normal_ctrl.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/button_normal_back"
android:top="2dip"
android:right="2dip"
android:bottom="2dip"
android:left="2dip"/>
<item
android:drawable="@drawable/button_normal_fore"
android:top="2dip"
android:right="2dip"
android:bottom="1dip"
android:left="2dip"/>
</layer-list>

Button_normal_back.xml and button_normal_fore handle similar highlighting and do not post code

 

The following figure shows the running effect:

Normal highlight

 

Of course, it may be an image rather than a character. In short, changes can be varied, and the core idea is the same.

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.