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: