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