Android custom dialog box (DIALOG) location, size

Source: Internet
Author: User

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 a 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 the setgravity parameter value is gravity. Left | gravity. Top, the dialog box appearsProgramBut in
* 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; // New Position y coordinate
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 screen width and height
// Windowmanager. layoutparams P = getwindow (). getattributes (); // Obtain the current parameter value of the dialog box.
// P. Height = (INT) (D. gethigh () * 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 >

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.