(reproduced) Android Project Combat (32): Rounded Corner dialog Box dialog

Source: Internet
Author: User

Objective:

A dialog box is used in the project, and the System dialog box is too ugly to write a custom dialog box yourself.

dialog box includes: 1, fillet

2, app icon, prompt text, close the "OK" button of the dialog box

Difficulty: 1, dialog box border fillet display

2, considering the hint text word number is not sure, in the case of not affecting aesthetics, you need to display the prompt text message in one line

3. Set the width and height of the dialog box

Technical Reserve:

1, Android Development _ Use Alertdialog Realization dialog box know Alertdialog have Setview (view), Dialog have Contentview (view) method.

2, Android Project Combat (V): TextView self-adapting to the size of a line display text information, when the text less words, the size of the text, when the text of more words, text size is small.

--------------------------------------------------------------------------------------------------------------- -----

1. Layout

Dialog_message.xml

Where the root container is used

   android:background= "@drawable/DIALOG_CORNER_BG"

This is shape to set the edge fillet

<?xml version= "1.0" encoding= "Utf-8"?><!--used to set the Information dialog box Fillet--><shape xmlns:android= "http/ Schemas.android.com/apk/res/android ">    <corners android:radius=" @dimen/dp_12 "></corners>    <solid android:color= "@color/white" ></solid></shape>

2, from the above can see the Settings dialog box fillet only need a drawable file, shape Set corners property.

Perhaps everyone from other articles found that some people use rounded background image to achieve the round corner of the dialog box, some people use style to achieve the dialog box fillet.

After my 1 hours of tossing and finding these methods are unreliable, in fact, it is very simple, the above method is Alertdialog, but we use the dialog class, a shape is sufficient.

Because there must be more dialog boxes in the project, I write a static method that passes the context parameters and the contents of the hint text:

public static void Showeditdialog (context context, String message) {}

  

1, initialize the dialog box related operations:

View view = Layoutinflater.from (context). Inflate (r.layout.dialog_message, null);        TextView confirm;    OK button        final TextView content;    Content        confirm = (TextView) View.findviewbyid (r.id.dialog_btn_comfirm);         Content = (TextView) View.findviewbyid (r.id.dialog_txt_content);        Content.settext (message);        Final Dialog Dialog = new Dialog (context);        Dialog.setcontentview (view);        Dialog.getwindow (). Setbackgrounddrawableresource (Android. r.color.transparent);  Set dialog box background transparent, for alertdialog it doesn't work.

  

2. Set the Click event for the OK button

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

3. Display dialog box

Dialog.show ();

  

4. Set the width and height of the dialog box

Displaymetrics DM = context.getresources (). Getdisplaymetrics ();        int displaywidth = dm.widthpixels;        int displayheight = dm.heightpixels;        Android.view.WindowManager.LayoutParams p = Dialog.getwindow (). GetAttributes ();  Gets the current parameter value of the dialog box        p.width = (int) (Displaywidth * 0.55);    The width is set to the screen of 0.55        p.height = (int) (displayheight * 0.28);    The height is set to the screen of 0.28        dialog.setcanceledontouchoutside (FALSE);//Setting the Tap Screen dialog does not disappear        Dialog.getwindow (). SetAttributes (p);     Settings take effect

Note: This is the width and height ratio of the screen to set the width and height of the dialog box.

There is one more way:

Alertdialog dialog = Builder.create ();  Dialog.setview (view);  Dialog.show ();  WindowManager m = Getwindowmanager ();    You will not be able  to invoke the Getwindowmanager () method if you are not in activity, so this method cannot use display d = M.getdefaultdisplay ();  To get the screen width, high     android.view.WindowManager.LayoutParams p = Dialog.getwindow (). GetAttributes ();  Gets the current parameter value of the dialog box     p.height = (int) (D.getheight () * 0.3);   The height is set to the screen of 0.3   p.width = (int) (D.getwidth () * 0.5);    The width is set to the 0.5    Dialog.getwindow () of the screen. SetAttributes (p);     Settings take effect  

A dialog box interface with more prompt text:

Full code:

 

  /*----------------------------Dialog---------------------------------*/public static void Showeditdialog (Context        Context, String message) {View view = Layoutinflater.from (context). Inflate (r.layout.dialog_message, NULL);    TextView confirm;    OK button final TextView content;        Content confirm = (TextView) View.findviewbyid (r.id.dialog_btn_comfirm);        Content = (TextView) View.findviewbyid (r.id.dialog_txt_content);        Content.settext (message);        Final Dialog Dialog = new Dialog (context);        Dialog.setcontentview (view); Dialog.getwindow (). Setbackgrounddrawableresource (Android.        R.color.transparent);                Confirm.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {            Dialog.dismiss ();        }        });        Dialog.show ();        Displaymetrics DM = context.getresources (). Getdisplaymetrics ();        int displaywidth = Dm.widthpixels; int Displayheight= Dm.heightpixels;  Android.view.WindowManager.LayoutParams p = Dialog.getwindow (). GetAttributes ();    Gets the current parameter value of the dialog box P.width = (int) (Displaywidth * 0.55);    The width is set to the screen of 0.5 p.height = (int) (displayheight * 0.28); The width is set to the screen of 0.5 dialog.setcanceledontouchoutside (false);//settings Tap the screen dialog not disappear Dialog.getwindow (). SetAttributes (P)     ; Set to take effect}

(reproduced) Android Project Combat (32): Rounded Corner dialog Box dialog

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.