Typical examples of android development ---- (1) Custom button implementation, android ----

Source: Internet
Author: User

Typical examples of android development ---- (1) Custom button implementation, android ----
Typical examples of android development ---- (1) Custom button implementation
I. Activity

MainActivity. java source code:


public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}


Ii. xml layout File

Activity_main.xml source code:


<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 code:

<?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>
In addition to styles in state_focused and state_pressed statuses, you can also set state_checked and state_selected statuses.
Bg_button_normal.png:

Bg_button_pressed.png:


Iv. Effect

(1) buttons in the natural state:


(2) buttons in the pressing status:





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.