Custom Control (4) custom click effect Button and control button

Source: Internet
Author: User

Custom Control (4) custom click effect Button and control button

In this section, you can customize the rounded rectangle Button with the click effect.
Function:
1. click the button to change color.
2. The text above is also click to change color
3. The button is in the shape of a rounded rectangle.
4. The button has a gradient effect.

It consists of five steps.:
First, you need to create a drawable directory (drawable unrelated to pixels) under the res directory ).
1. Set the button to normal

// Button_shape_normal.xml <? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android" android: shape = "rectangle"> <! -- Set to rectangle --> <corners android: radius = "3dp"/> <! -- Set the radius of the rounded rectangle --> <gradient android: angle = "90" android: endColor = "# cccccc" android: startColor = "# acacac"/> <! -- Specify the color gradient (Starting color, ending color, and Gradient Direction) --> </shape>

2. Set button click status

// Button_shape_pressed.xml <? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android" android: shape = "rectangle"> <corners android: radius = "3dp"/> <! -- Set the radius of the rounded rectangle --> <gradient android: angle = "270" android: endColor = "# cccccc" android: startColor = "# acacac"/> <! -- Specify the color gradient (Starting color, ending color, and Gradient Direction) --> </shape>

3. Set the selector of the Button.

//button_shape_selector.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/button_shape_normal"        android:state_pressed="false"/>    <item android:drawable="@drawable/button_shape_pressed"        android:state_pressed="true"/></selector>

4. Set the text color selector

//button_text_color.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_pressed="false" android:color="#ffffff"/>    <item android:state_pressed="true" android:color="#556699"/></selector>

5. Reference The resource you just created in the button attribute

<Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="wangjiang"        android:textSize="16sp"        android:background="@drawable/button_shape_selector"         android:textColor="@drawable/button_text_color"/>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.