Code:
Package angel. dedevil;
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 class DialogDemoActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Dialog dialog = new Dialog (this );
// SetContentView can be set to a View or the resource ID can be simply specified.
// 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 and parameter object of the Christmas box to modify the layout settings of the dialog box,
* You can directly call getWindow () to obtain the Window of the Activity.
* Object. In this way, the attribute of the Activity can be changed in the same way.
*/
Window dialogWindow = dialog. getWindow ();
WindowManager. LayoutParams lp = dialogWindow. getAttributes ();
DialogWindow. setGravity (Gravity. LEFT | Gravity. TOP );
/*
* Lp. x and lp. y indicate the offset relative to the original position.
* When the parameter value contains Gravity. LEFT, the dialog box appears on the LEFT. Therefore, lp. x indicates the offset relative to the LEFT. The negative value is ignored.
* When the parameter value contains Gravity. RIGHT, the dialog box appears on the RIGHT. Therefore, lp. x indicates the offset relative to the RIGHT. The negative value is ignored.
* When the parameter value contains Gravity. TOP, the dialog box appears above, so lp. y indicates the offset relative to the TOP, and the negative value is ignored.
* When the parameter value contains Gravity. BOTTOM, the dialog box appears below, so lp. y indicates the offset relative to the BOTTOM, and the negative value is ignored.
* When the parameter value contains Gravity. CENTER_HORIZONTAL
* The dialog box is horizontally centered. Therefore, lp. x indicates moving the lp. x pixel at the horizontally centered position. The positive value moves to the right and the negative value moves to the left.
* When the parameter value contains Gravity. CENTER_VERTICAL
* The dialog box is vertically centered. Therefore, lp. y indicates moving the lp. y pixel at the vertical center position, moving the positive value to the right, and moving the negative value to the left.
* The default value of gravity is Gravity. CENTER, that is, Gravity. CENTER_HORIZONTAL |
* Gravity. CENTER_VERTICAL.
*
* When setGravity is set to Gravity. LEFT | Gravity. TOP, the dialog box appears in the upper LEFT corner of the program,
* I found a short distance between the left side and the top side during the test on my mobile phone, and the vertical coordinates included the program title bar,
* Gravity. LEFT, Gravity. TOP, Gravity. BOTTOM and Gravity. RIGHT are both in this case. There is a short distance between the data boundary.
*/
Lp. x = 100; // new position X coordinate
Lp. y = 100; // Y coordinate of the new position
Lp. width = 300; // width
Lp. height = 300; // height
Lp. alpha = 0.7f; // transparency
// When the Attributes of Window changes, the system will call this function. You can directly call the changes to Window parameters on the application, or use setAttributes.
// Dialog. onalog wattributeschanged (lp );
DialogWindow. setAttributes (lp );
/*
* Set the dialog box size by the percentage of the screen size
*/
// WindowManager m = getWindowManager ();
// Display d = m. getDefaultDisplay (); // obtain the screen width and height
// WindowManager. LayoutParams p = getWindow (). getAttributes (); // obtain the current parameter value in the dialog box
// P. height = (int) (d. getHeight () * 0.6); // set the height to 0.6 of the screen.
// P. width = (int) (d. getWidth () * 0.65); // set the width to 0.95 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>
Author: angeldedevil