Location and size examples for customizing dialog in Android

Source: Internet
Author: User
Tags relative xmlns

In writing code to use the dialog box, many times we need to build the layout of the dialog box, that is, to customize the dialog, and then run out of effect, often the size of the dialog box is out of proportion, location is also the default center, it is not in line with our needs, Here's a section of code that defines the location and size of the dialog box.

Example 1

Import android.app.Activity;
Import Android.app.Dialog;
Import Android.os.Bundle;
Import Android.view.Display;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.widget.TextView;

public class Mainactivity extends activity {
TextView TV;
TextView TV2;
Private Dialog Dialog;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
tv= (TextView) Findviewbyid (r.id.tv);
Tv.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Dialog.show ();
}
});
Load dialog box
View view = View.inflate (Mainactivity.this,
R.layout.dialog_register, NULL);

Dialog = New dialog (Mainactivity.this, R.style.mydialogstylebottom);
Dialog.setcanceledontouchoutside (FALSE);
Tv2= (TextView) View.findviewbyid (R.id.tv_yes);
Dialog.setcontentview (view);

Tv2.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Dialog.dismiss ();
}
});

Window Dialogwindow = Dialog.getwindow ();
Windowmanager.layoutparams LP = Dialogwindow.getattributes ();
Dialogwindow.setgravity (Gravity.center);
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.3); Height is set to 0.3 of the screen
P.width = (int) (D.getwidth () * 0.85); Width set to 0.85 of the screen
Dialogwindow.setattributes (P);

}
}

Example 2

Custom dialog box (Dialog) location, size
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 the activity is a. */
@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 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.
* You can call GetWindow () directly to indicate the window that gets the activity
* Object so that it 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, so the lp.x represents the offset to the left, and the negative value is ignored.
* When the parameter value contains Gravity.right, the dialog box appears on the right, so the lp.x represents the offset to the right, and the negative value is ignored.
* When the parameter value contains Gravity.top, the dialog box appears above, so lp.y represents the offset from the top, and negative values are ignored.
* When the parameter value contains Gravity.bottom, the dialog box appears below, so the 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 lp.x represents moving lp.x pixels in a horizontally centered position, with positive values moving to the right and negative values moving to the left.
* When the parameter value contains gravity.center_vertical
*, the dialog box is centered vertically, so lp.y represents moving the lp.y pixel in the vertical center position, the positive value moves to the right, and the negative value moves to the left.
* The default value for gravity is Gravity.center, that is Gravity.center_horizontal |
* Gravity.center_vertical.
*
* The parameter value of the original setgravity is Gravity.left | The dialog box should appear in the upper-left corner of the program when Gravity.top, but
* My phone on the test found a small distance from the left and the top, and the vertical coordinates of the program title bar also counted,
* Gravity.left, Gravity.top, Gravity.bottom and Gravity.right are the same, according to the border of a small distance
*/
lp.x = 100; New position x coordinates
LP.Y = 100; New position y-coordinate
Lp.width = 300; Width
Lp.height = 300; Height
Lp.alpha = 0.7f; Transparency

This function is called when the window's attributes changes, and can be invoked directly to apply changes to the window's parameters or SetAttributes
DIALOG.ONWINDOWATTRIBUTESCHANGED (LP);
Dialogwindow.setattributes (LP);

/*
* Set the size of the dialog box by 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 is 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>

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.