Code:
Package angel.devil; import android.app.activity;import android.app.dialog;import Android.os.bundle;import Android.view.gravity;import Android.view.window;import Android.view.WindowManager; Public classDialogdemoactivity extends Activity {/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Dialog Dialog=NewDialog ( This); //Setcontentview can be set to a view or you can simply specify a resource ID//Layoutinflater//li= (Layoutinflater) Getsystemservice (Layout_inflater_service); //View v=li.inflate (r.layout.dialog_layout, NULL); //Dialog.setcontentview (v);Dialog.setcontentview (r.layout.dialog_layout); Dialog.settitle ("Custom Dialog"); /** Get the Window object of the Christmas box and the parameter object to modify the layout settings of the dialog, * you can call GetWindow () directly, indicating the window object to get the activity, so this can change the A in the same way The properties of the ctivity. */Window Dialogwindow=Dialog.getwindow (); Windowmanager.layoutparams LP=dialogwindow.getattributes (); Dialogwindow.setgravity (Gravity.left|gravity.top); /** lp.x and lp.y represent offsets relative to the original position. * When the parameter value contains Gravity.left, the dialog box appears on the left side, so the lp.x represents a relative left offset and a negative value is ignored. * When the parameter value contains Gravity.right, the dialog box appears on the right side, so lp.x represents the opposite right offset, negative values are ignored. * When the parameter value contains Gravity.top, the dialog box appears on top, so lp.y represents the relative top offset and negative values are ignored. * When the parameter value contains Gravity.bottom, the dialog box appears below, so lp.y represents the relative bottom offset and negative values are ignored. * When the parameter value contains Gravity.center_horizontal *, the dialog box is centered horizontally, so the lp.x means moving the lp.x pixel in the horizontal center position, positive value moving to the right and negative to the left. * When the parameter value contains gravity.center_vertical *, the dialog box is centered vertically, so the lp.y means moving the lp.y pixels in a vertically centered position, the positive values moving to the right, and negative values moving to the left. * The default value for Gravity is Gravity.center, which is Gravity.center_horizontal | * Gravity.center_vertical. * * Original setgravity parameter value is Gravity.left | Gravity.top when the dialog box should appear in the upper left corner of the program, but on my phone test found that the left and the top have a small distance, and the vertical coordinates of the program title bar is also counted, * gravity.left, Gravity.top, Gra Vity. Bottom and gravity.right are so, according to the boundary there is a short distance*/lp.x= -;//new position x coordinateLP.Y = -;//new position y-coordinateLp.width = -;//widthLp.height = -;//HeightLp.alpha =0.7f;//Transparency//This function is called by the system when the attributes of the window changes, and can be called directly to apply changes to the window parameters, or SetAttributes//dialog.onwindowattributeschanged (LP);dialogwindow.setattributes (LP); /** Set the size of the dialog box as a percentage of the screen size*///WindowManager m = Getwindowmanager ();//Display d = m.getdefaultdisplay ();//get screen width, high-use//Windowmanager.layoutparams p = dialogwindow.getattributes ();//gets the current parameter value of the dialog box//p.height = (int) (D.getheight () * 0.6);//height set to 0.6 of the screen//p.width = (int) (D.getwidth () * 0.65);//width set to 0.65 of the screen//dialogwindow.setattributes (p);dialog.show (); }}
Layout file:
Main.xml
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:background="#00FF00"android:orientation="Vertical"> <TextView android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:text="@string/hello"/> </LinearLayout>
Dialog_layout.xml
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:id="@+id/layout_root"Android:layout_width="fill_parent"Android:layout_height="fill_parent"android:orientation="Horizontal"android:padding="10DP"> <ImageView Android:id="@+id/image"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_marginright="10DP"android:src="@drawable/ic_launcher"/> <TextView Android:id="@+id/text"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="A Dialog"Android:textcolor="#FFF"/> </LinearLayout>
Android Custom dialog box (Dialog) location, size