[Android] Android custom dialog box (DIALOG) location, size

Source: Internet
Author: User

The article is found out, the original source: http://www.cnblogs.com/angeldevil/archive/2012/03/31/2426242.html

The focus of this article is that the x/y value of windowmanager. layoutparams is the relative value associated with gravity when you customize the dialog position. Based on the original text, it is added that if the x/y value is out of the screen range, the dialog will not be completely displayed or appear outside the screen, and will still be completely displayed at the edge of the screen.

For the question raised in the original article: "a short distance from the boundary ". This is because the dialog background image has a translucent transition region, which is already part of the dialog itself, let's take a look at Part 2 of my historical article "Android's interesting and fully transparent effect-full transparency of activity and dialog (with the built-in icons of Android. the first figure in "dialog all transparency" can be explained.

The Code is as follows:

Package lab. sodino. dialoglab; 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 actdialog extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. act_dialog); dialog = new dialog (this); dialog. setcontentview (R. layout. dialog_layout); dialog. settitle ("Custom dialog"); dialog. show ();/** 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 * object of the activity, in this way, the attributes of the activity can be changed in the same way. */window dialogwindow = dialog. getwindow (); windowmanager. layoutparams Lp = dialogwindow. getattributes (); dialogwindow. setgravity (gravity. center);/** LP. X and LP. Y indicates the offset relative to the original position. * The value of the parameter contains gravity. left, the dialog box appears on the left, so LP. X indicates the offset relative to the left, and the negative value is ignored. * The value of the parameter contains gravity. right, the dialog box appears on the right, so LP. X indicates the offset relative to the right. The negative value is ignored. * The value of the parameter contains gravity. top, the dialog box appears above, so LP. Y indicates the offset relative to the top, and the negative value is ignored. * The value of the parameter contains gravity. at bottom, the dialog box appears below, so LP. Y indicates the offset relative to the bottom, and the negative value is ignored. * The value of the parameter contains gravity. when center_horizontal *, the dialog box is horizontally centered, so LP. X indicates moving the LP at the horizontal center. X pixel. The positive value moves to the right, and the negative value moves to the left. * The value of the parameter contains gravity. when center_vertical *, the dialog box is vertically centered, so LP. Y indicates moving the LP at the vertical center. Y pixel, positive value moves to the right, negative value moves to the left. * The default value of gravity is gravity. center, that is, gravity. center_horizontal * | gravity. center_vertical. ** the parameter value of setgravity is gravity. left | gravity. the top dialog box should appear in the upper left corner of the program, but I found a short distance between the left and the top in * my mobile phone test, and the vertical coordinates also included the program title bar, gravity. left, gravity. top, * gravity. bottom and gravity. this is true for right. There is a short distance from the boundary */LP. X = 500; // new position X coordinate LP. y = 200; // New Position y coordinate LP. width = 300; // The LP width. height = 300; // height LP. alpha = 0.1f; // 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, you can also use setattributes // dialog. ondeskwattributeschanged (LP); dialogwindow. setattributes (LP );}}

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>

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.