Android custom dialog box (Dialog) location, size

Source: Internet
Author: User

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 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 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 and parameter object of the Christmas box to modify the layout settings of the dialog box.
* The GetWindow () can be called directly to indicate the window where the activity was obtained.
* Object so that this can change the properties of the activity in the same way.
*/
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 a horizontally centered position, the positive value moving to the right and the negative moving 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 pixel in a vertically centered position, the positive value moving to the right and the negative 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 the dialog box should appear in the upper-left corner of the program, but
* I found a small distance between the left and top of the test on my phone, and the vertical coordinates also counted the program title bar,
* Gravity.left, Gravity.top, Gravity.bottom and gravity.right are so, according to the border there is a short distance
*/
lp.x = 100; New position X coordinate
LP.Y = 100; New position y-coordinate
Lp.width = 300; Width
Lp.height = 300; Height
Lp.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, Gao Yun
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 is 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.