Android Project Combat (32): Round Corner dialog Box dialog

Source: Internet
Author: User

Original: Android Project Combat (32): Round Corner dialog Box dialog

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

<?xml version="1.0"encoding="Utf-8"? ><relativelayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="match_parent"Xmlns:autofit="Http://schemas.android.com/apk/res-auto"Android:layout_height="wrap_content"Android:background="@drawable/DIALOG_CORNER_BG"Android:paddingbottom="@dimen/dp_16"> <ImageView Android:id="@+id/dialog_img"Android:layout_width="30DP"Android:layout_height="30DP"android:src="@mipmap/icon1"Android:layout_margintop="@dimen/dp_12"Android:layout_centerhorizontal="true"/> <Me.grantland.widget.AutofitTextView Android:id="@+id/dialog_txt_content"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:singleline="true"Android:maxlines="1"android:textsize="14SP"autofit:mintextsize="10SP"Android:text="download failed, please try again"android:gravity="Center"Android:layout_margin="@dimen/dp_6"android:layout_centerinparent="true"/> <TextView Android:id="@+id/dialog_btn_comfirm"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Determine"android:gravity="Center"Android:background="@drawable/bg_btn_blue_big"Android:textcolor="@color/white"Android:paddingtop="@dimen/dp_6"Android:paddingbottom="@dimen/dp_6"Android:paddingleft="@dimen/dp_30"Android:paddingright="@dimen/dp_30"Android:layout_centerhorizontal="true"Android:layout_alignparentbottom="true"/></relativelayout>
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"?><!-- Fillet--><shape xmlns:android="http://schemas.android.com/apk/res/android" For setting Information dialog box >    <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          //  OK button  final TextView content; //  content  confirm = (TextView) View.findviewbyid (r.id.di         ALOG_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); //Settings 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 (); intDisplaywidth =Dm.widthpixels; intDisplayheight =Dm.heightpixels; Android.view.WindowManager.LayoutParams P= Dialog.getwindow (). GetAttributes ();//gets the current parameter value of the dialog boxP.width = (int) (Displaywidth *0.55);//width set to 0.55 of the screenP.height = (int) (Displayheight *0.28);//High0.28 of the screen is set toDialog.setcanceledontouchoutside (false);//Set the Tap Screen dialog not disappearDialog.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 call the Getwindowmanager () method if you are not in the activity, so this method cannot use the Display D= M.getdefaultdisplay ();//to get the screen width, heightAndroid.view.WindowManager.LayoutParams p = Dialog.getwindow (). GetAttributes ();//gets the current parameter value of the dialog boxP.height = (int) (D.getheight () *0.3);//height set to 0.3 of the screenP.width = (int) (D.getwidth () *0.5);//width set to 0.5 of the screenDialog.getwindow (). SetAttributes (P);//settings take effect

A dialog box interface with more prompt text:

Full code:

 

  /*----------------------------Dialog---------------------------------*/     Public Static voidShoweditdialog (Context context, String message) {View View= Layoutinflater. from(context). Inflate (R.layout.dialog_message,NULL);    TextView confirm; //OK buttonFinal TextView content;//contentConfirm =(TextView) View.findviewbyid (r.id.dialog_btn_comfirm); Content=(TextView) View.findviewbyid (r.id.dialog_txt_content);        Content.settext (message); Final Dialog Dialog=NewDialog (context);        Dialog.setcontentview (view); Dialog.getwindow (). Setbackgrounddrawableresource (Android.        R.color.transparent); Confirm.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Dialog.dismiss ();        }        });        Dialog.show (); Displaymetrics DM=context.getresources (). Getdisplaymetrics (); intDisplaywidth =Dm.widthpixels; intDisplayheight =Dm.heightpixels; Android.view.WindowManager.LayoutParams P= Dialog.getwindow (). GetAttributes ();//gets the current parameter value of the dialog boxP.width = (int) (Displaywidth *0.55);//width set to 0.5 of the screenP.height = (int) (Displayheight *0.28);//width set to 0.5 of the screenDialog.setcanceledontouchoutside (false);//Set the Tap Screen dialog not disappearDialog.getwindow (). SetAttributes (P);//settings take effect    }

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

I currently singled out two apps, responsible for Leancloud cloud development, students can pay attention to my little programmer, have problems to help each other solve.

 

Android Project Combat (32): Round 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.