android-Custom Dialog

Source: Internet
Author: User

Customizing dialog location and size2012-12-06 11:562835 People readComments (0)CollectionReportDialogdialog

directory (?) [+]


 PackageAngel.devil;

Importandroid.app.Activity;
ImportAndroid.app.Dialog;
ImportAndroid.os.Bundle;
Importandroid.view.Gravity;
ImportAndroid.view.Window;
ImportAndroid.view.WindowManager;

Public classDialogdemoactivityextendsActivity {
/**Called when the activity is first created.*/
@Override
Public voidOnCreate (Bundle savedinstancestate) {
Super. OnCreate (Savedinstancestate);
Setcontentview (R.layout.main);

Dialog Dialog =NewDialog ( 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 = GetWindow (). 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.95 of the screen
//dialogwindow.setattributes (p);

Dialog.show ();

}
}

Layout file:

Main.xml

<? XML version= "1.0" encoding= "Utf-8" ?>
<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"?>
<LinearLayoutxmlns: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>
setting the size of Alertdialog in AndroidCategory: Android2012-02-10 15:47345 people readComments (0)CollectionReport [Java]View Plaincopy
    1. Alertdialog Dialog = Builder.settitle ("message list")
    2. . Setview (Layout)
    3. . Create ();
    4. Dialog.show ();
    5. //Set the size of the window   
    6. Dialog.getwindow (). setlayout ( +);


Dialog.show (); Be sure to place the Dialog.getwindow (). setlayout (300, 200); the front, otherwise, does not work.

Online there is a way to [Java]View Plaincopy
    1. Windowmanager.layoutparams params = Dialog.getwindow (). GetAttributes ();
    2. Params.width = +;
    3. Params.height = $;
    4. Dialog.getwindow (). SetAttributes (params);
But Dialog.getwindow (). setlayout (300, 200); Actually encapsulates this method, the source code for setlayout () is as follows: [Java]View Plaincopy
    1. Final  windowmanager.layoutparams attrs = GetAttributes ();
    2. Attrs.width = width;
    3. Attrs.height = height;
    4. if (mcallback! = null) {
    5. Mcallback.onwindowattributeschanged (ATTRS);
    6. }

So the two methods are essentially the same, both of which are set to Alertdialog size
  1. WindowManager m = Getwindowmanager ();
  2. Display d = m.getdefaultdisplay (); //To get the screen width, height   
  3. Layoutparams p = GetWindow (). GetAttributes (); //Gets the current parameter value of the dialog box   
  4. P.height = (int) (D.getheight () * 0.6); //height set to 0.6 of the screen
  5. P.width = (int) (D.getwidth () * 0.95); //Width set to 0.95 of the screen
  6. GetWindow (). SetAttributes (P); //settings take effect   

public class Test_dialog extends dialog{

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.dialog_testinginput);

Set size

Layoutparams params = GetWindow (). GetAttributes ();

Params.height = layoutparams.fill_parent;

Params.width = layoutparams.fill_parent;

GetWindow (). SetAttributes ((android.view.WindowManager.LayoutParams) params);

//----------------------------------

}

}

================================================================= custom Alertdialog style, display according to screen sizelandlordposted in 2011-12-20 19:39:45|Views: 1608|Replies: 4
Let's introduce some basic knowledge about Alertdialog:

Alertdialog Introduction: The Alertdialog constructor method is declared as protected, so you cannot use the New keyword directly to create an object instance of the Alertdialog class. To create the Alertdialog dialog box, you need to use the builder class, which is an inline class defined in the Alertdialog class. Therefore, you must create an object instance of the Alertdialog.builder class and then call Show () to display the dialog box.

Ii. types of dialog boxes created using Alertdialog:

1. A dialog box with up to 3 buttons: Setpositivebutton (...) --Confirmation, Setnegativebutton (...) --Cancel, Setneutralbutton (...) --Ignore

2. Simple List dialog box: Setitems (...) through the Alertdialog.builder class. method to create a simple List dialog box. In fact, this type of dialog box is equivalent to placing the ListView component on a dialog box, and then adding some simple text in the ListView.

3. Radio List dialog box: Setsinglechoiceitems (...) through the Alertdialog.builder class To create. Currently supports 4 types of data sources (array resources, datasets, string arrays, ListAdapter)

4. Multi-select list dialog box: Setmultichoiceitems (...) through the Alertdialog.builder class. Create. Currently supports 3 types of data sources (array resources, datasets, string arrays)

5. Horizontal Progress or Circular dialog box (default: Circular): This type of dialog is implemented by ProgressDialog, which is a subclass of Alertdialog, which does not need to use the Create () method to return an instance object, just need new.

Progressdialog.style_horizontal//Horizontal Progress style

Progressdialog.style_spinner//Round Style

6. Custom dialogs: Create views directly using an XML layout file or write Java code, and add these view objects to the dialog box.

7. Use the Activity Hosting dialog: The Activity class also provides a way to create a dialog box, with a oncreatedialog (int id) method whose return type is dialog, by invoking the showdialog of the Activity class ( int id) method, the system calls the method to return a dialog object. Both ShowDialog and Oncreatedialog have an ID parameter of type int, which is passed to the Oncreatedialog method. Therefore, we can create multiple dialog boxes with different IDs.

Note: For an ID that represents a dialog box, the system calls the Oncreatedialog method only the 1th time the ShowDialog method is called. The 1th time the dialog object is created, the object is saved in the activity's cache, which is the equivalent of a map object, the ID of the dialog box as the map key, and the dialog object as the value of the map. The next time the call is made, the dialog object created for the 1th time is obtained from the map based on this ID. Unless the ID has been deleted.

8. Hover the dialog box and touch any location can close the dialog box:

1). Hover dialog: Android:theme= "@android: Style/theme.dialog"; For this type of dialog box, touching the screen anywhere will trigger the activity's Ontouchevent event.

2). Touch the dialog box that can be closed anywhere: You must first inherit the Alertdialog class and override the Ontouchevent event.

The first type:
Java code
  1. /**
  2. * Custom Alertdialog
  3. *
  4. * @author Chenjianli 2011-05-10
  5. */
  6. public void alert () {
  7. WindowManager manager = Getwindowmanager ();
  8. Display display = Manager.getdefaultdisplay ();
  9. int width = display.getwidth ();
  10. int height = display.getheight ();
  11. Layoutinflater inflater = Getlayoutinflater ();
  12. View view = Inflater.inflate (R.layout.alert, NULL);
  13. TextView Text = (TextView) View.findviewbyid (R.id.text);
  14. Text.settext ("Custom Alertdialog");
  15. Alertdialog alert = new Alertdialog.builder (this). Create ();
  16. Alert.show ();
  17. Alert.getwindow (). setlayout (WIDTH/2, HEIGHT/4);
  18. Alert.settitle ("test");
  19. Alert.getwindow (). Setcontentview (R.layout.alert);
  20. }
Copy CodeThe second type:
Java code
    1. /**
    2. * Custom Alertdialog
    3. *
    4. * @author Chenjianli 2011-05-10
    5. */
    6. Alertdialog Zidongbofangdialog = new Alertdialog.builder (manhuaactivity.this). Create ();
    7. Zidongbofangdialog.show ();
    8. Zidongbofangdialog.getwindow (). setgravity (Gravity.center);
    9. Zidongbofangdialog.getwindow (). setlayout (
    10. Android.view.WindowManager.LayoutParams.FILL_PARENT,
    11. Android.view.WindowManager.LayoutParams.WRAP_CONTENT);
    12. Zidongbofangdialog.getwindow (). Setcontentview (R.layout.manhua_dialog_zidongbofang);
Copy CodeThe third type:
/**
* Custom Alertdialog
*
* @author Chenjianli 2011-05-10
*/
If we Setview (), in the view is with EditText, at this time, we must add such a word before show () before you can click the EditText pop-up keyboard, otherwise it will be a cup! The keyboard is not elastic.
Java code
    1. Alertdialog tiaozhuandialog= New Alertdialog.builder (manhuaactivity.this). Create ();
    2. Tiaozhuandialog.setview (Getlayoutinflater (). Inflate (R.layout.manhua_dialog_tiaozhuan, null));
    3. Tiaozhuandialog.show ();
    4. Tiaozhuandialog.getwindow (). setgravity (Gravity.center);
    5. Tiaozhuandialog.getwindow (). setlayout (
    6. Android.view.WindowManager.LayoutParams.FILL_PARENT,
    7. Android.view.WindowManager.LayoutParams.WRAP_CONTENT);
    8. Tiaozhuandialog.getwindow (). Setcontentview (Getlayoutinflater (). Inflate (R.layout.manhua_dialog_tiaozhuan, NULL)) ;
Copy CodeThere is another place to note that if we need to set the contents of the EditText in the view shown in the Alertdialog before we show this alertdialog, we should go to Findviewbyid ():
Java code
    1. EditText EditText = (EditText) Tiaozhuandialog.findviewbyid (R.id.myedittext);
    2. Edittext.settext ("Who is?") I am Android Developer ");
Copy CodeOtherwise it will be reported error/androidruntime (1032): Java.lang.IllegalStateException:The specified child already have a parent. You must call Removeview () on the child ' s parent first. Error!!

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.