Write a demo to review the custom dialog box and state selector
First up effect
Source: http://download.csdn.net/detail/qq_35463672/9591065
In the first step, two pictures are placed under drawable-hdpi, and in res folder drawable the state selector in which Img.xml is created
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/fire "android:state_pressed=" true "/> <!--pressed-- > <item android:drawable= "@drawable/fire" android:state_focused= "true"/> <!--focused -- default --></selector>
Three states for different pictures
Step two: Create Mydialog to try to
<?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" > <TextView android:gravity= "Center"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Small Flame"/> <!--Be sure to add this sentence android:clickable= "true"-<ImageView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:clickable= "true"Android:background= "@drawable/img"/></linearlayout>
Note that you must add this android:clickable= "true" to respond to a click and the state selector is valid
Step Three: Write Mainactivity
Packagecom.example.mytest002;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.app.AlertDialog.Builder;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Button button1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button1=(Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {ShowDialog (); } }); } Public voidShowDialog () {Alertdialog.builder Builder=NewBuilder ( This); Alertdialog Dialog=builder.create (); View View=view.inflate ( This, R.layout.my_dialog,NULL); //remove Upper and lower left and right margins, compatible with 2.x versionDialog.setview (View, 0,0,0,0); Dialog.show (); } }
Customize dialog box plus status selector