Android Development most common examples of finishing----(1) Custom button implementation

Source: Internet
Author: User

Android Development most common examples of finishing----(1) Custom button implementation
First, Activity

Mainactivity.java Source:


public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}}


Second, XML layout file

Activity_main.xml Source:


<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= "Wrap_content"        android:layout_height= "wrap_content"        android:background= "@drawable /bg_button "/></linearlayout>


Iii. Related Resources

Files under res/drawable/:


Bg_button.xml Source:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_focused=" true "android:drawable=" @drawable/bg_button_pressed "></ Item><item android:state_pressed= "true" android:drawable= "@drawable/bg_button_pressed" ></item> <item  android:drawable= "@drawable/bg_button_normal" ></item></selector>
Add: In addition to the state_focused, state_pressed can be set in the state of the style, you can also set state_checked, state_selected and other states.
Bg_button_normal.png:

Bg_button_pressed.png:


Four, the effect

(1) button in the natural state:


(2) Press the button in the state:





The most common examples of Android development----(1) custom button implementations

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.