Android Custom Button

Source: Internet
Author: User

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

 

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.