Android Custom Button can be divided into two forms:
1. inherit the Button by customizing the MyButton class and implement all the effects in the class.
2. Use the xml file to change the style and color of the Button.
Today, I will first talk about using xml files, and then encapsulate the Custom Button class later.
TestcActivity
[Html]
Package com. example. blueapp;
Import android. app. Activity;
Import android. OS. Bundle;
Public class TestcActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. testc );
}
}
Package com. example. blueapp;
Import android. app. Activity;
Import android. OS. Bundle;
Public class TestcActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. testc );
}
}
Testc. xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "20dp"
Android: text = "test button"
Android: background = "@ drawable/button_style"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "20dp"
Android: text = "original button"
/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "20dp"
Android: text = "test button"
Android: background = "@ drawable/button_style"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "20dp"
Android: text = "original button"
/>
</LinearLayout>
Button_style.xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: state_pressed = "true">
<Shape>
<Gradient android: startColor = "#0d76e1" android: endColor = "#0d76e1"
Android: angle = "270" type = "parmname" text = "parmname"/>
<Stroke android: width = "1dip" android: color = "# f403c9"/>
<Corners android: radius = "2dp"/>
<Padding android: left = "10dp" android: top = "10dp"
Android: right = "10dp" android: bottom = "10dp"/>
</Shape>
</Item>
<Item android: state_focused = "true">
<Shape>
<Gradient android: startColor = "# ffc2b7" android: endColor = "# ffc2b7"
Android: angle = "270" type = "parmname" text = "parmname"/>
<Stroke android: width = "1dip" android: color = "# f403c9"/>
<Corners android: radius = "2dp"/>
<Padding android: left = "10dp" android: top = "10dp"
Android: right = "10dp" android: bottom = "10dp"/>
</Shape>
</Item>
<Item>
<Shape>
<Gradient android: startColor = "#000000" android: endColor = "# ffffff"
Android: angle = "180" type = "parmname" text = "parmname"/>
<Stroke android: width = "1dip" android: color = "# f403c9"/>
<Corners android: radius = "5dip"/>
<Padding android: left = "10dp" android: top = "10dp"
Android: right = "10dp" android: bottom = "10dp"/>
</Shape>
</Item>
</Selector>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: state_pressed = "true">
<Shape>
<Gradient android: startColor = "#0d76e1" android: endColor = "#0d76e1"
Android: angle = "270" type = "parmname" text = "parmname"/>
<Stroke android: width = "1dip" android: color = "# f403c9"/>
<Corners android: radius = "2dp"/>
<Padding android: left = "10dp" android: top = "10dp"
Android: right = "10dp" android: bottom = "10dp"/>
</Shape>
</Item>
<Item android: state_focused = "true">
<Shape>
<Gradient android: startColor = "# ffc2b7" android: endColor = "# ffc2b7"
Android: angle = "270" type = "parmname" text = "parmname"/>
<Stroke android: width = "1dip" android: color = "# f403c9"/>
<Corners android: radius = "2dp"/>
<Padding android: left = "10dp" android: top = "10dp"
Android: right = "10dp" android: bottom = "10dp"/>
</Shape>
</Item>
<Item>
<Shape>
<Gradient android: startColor = "#000000" android: endColor = "# ffffff"
Android: angle = "180" type = "parmname" text = "parmname"/>
<Stroke android: width = "1dip" android: color = "# f403c9"/>
<Corners android: radius = "5dip"/>
<Padding android: left = "10dp" android: top = "10dp"
Android: right = "10dp" android: bottom = "10dp"/>
</Shape>
</Item>
</Selector>
Gradient main gradient startColor start color, endColor end color, angle start gradient angle (the value can only be a multiple of 90, 0 is a left-right gradient, 90 is a bottom-up gradient, and so on)
Stroke Border width, color border color
Radius of the corners rounded corner. 0 is the right angle.
Relative Position of the padding text Value