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.