"Go" Android alertdialog custom layout

Source: Internet
Author: User

Original website: 53022294

Since the development of the frame is often used, but the system comes with the frame is too limited, not too beautiful, often can not meet the development needs, so you can only customize the layout. In fact, the custom layout is very simple, not to write it out, but if you do not write it again, the following words will feel forgotten, so in the second note a small pen, only one of the simplest example, you can extrapolate.

Directly on the code

PublicClassMainactivityExtendsActivityImplementsOnclicklistener {Private TextView Text1, Text2;Private Context Mcontext;@OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Mcontext =This Initview (); }PrivatevoidInitview () {Text1 = (TextView) Findviewbyid (R.ID.TEXT1); text2 = (TextView) Findviewbyid (R.ID.TEXT2); Text1.setonclicklistener (this); Text2.setonclicklistener (this); }@OverridePublicvoidOnClick (View v) {Switch (V.getid ()) {Case R.id.text1:dialogshow1 ();BreakCase R.id.text2:dialogshow2 ();BreakDefaultBreak } }PrivatevoidDialogShow1 () {Alertdialog.builder Builder =New Builder (Mcontext); Builder.settitle ("Warm Tips"); Builder.seticon (R.drawable.ic_launcher); Builder.setmessage ("Principle is basic"); Builder.setnegativebutton ("Cancel",New Dialoginterface.onclicklistener () {@OverridePublicvoidOnClick (Dialoginterface arg0,int arg1) {Toast.maketext (Mcontext,"No",1). Show (); } }); Builder.setpositivebutton ("Update Now",New Dialoginterface.onclicklistener () {@OverridePublicvoidOnClick (Dialoginterface arg0,int arg1) {Toast.maketext (Mcontext,"OK",1). Show (); } }); Dialog Dialog = Builder.create (); Dialog.show (); }/** * Custom layout * Setview () will only overwrite the part of the alertdialog between the title and the button, and Setcontentview () will overwrite all, * Setcontentview () must be placed behind show () */PrivatevoidDialogShow2 () {Alertdialog.builder Builder =New Builder (Mcontext); Layoutinflater inflater = Layoutinflater.from (Mcontext); View v = inflater.inflate (R.layout.update_manage_dialog,NULL); TextView content = (TextView) V.findviewbyid (r.id.dialog_content); Button btn_sure = (button) V.findviewbyid (r.id.dialog_btn_sure); Button Btn_cancel = (button) V.findviewbyid (R.id.dialog_btn_cancel);Builer.setview (v);//If you use Builer.setview (v), the custom layout will only overwrite the part between the title and the buttonFinal Dialog Dialog = Builder.create (); Dialog.show (); Dialog.getwindow (). Setcontentview (v);//Custom layout should be added here, after Dialog.show () //dialog.getwindow (). setgravity (Gravity.center);//You can set the location of the display Btn_ Sure.setonclicklistener (new Onclicklistener () { @Override public void OnClick (View v) { Dialog.dismiss (); Toast.maketext (Mcontext, "OK", 1). Show ();}); Btn_cancel.setonclicklistener (new Onclicklistener () { @Override public void OnClick (View arg0 ) {Dialog.dismiss (); Toast.maketext (Mcontext, "No", 1). Show ();}); }}

Layout of the Activity_main

<LinearLayoutXmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:layout_margintop="100DP"android:orientation="Vertical" ><TextViewAndroid:id="@+id/text1"Android:layout_width="Match_parent"android:layout_height="Wrap_content"android:layout_margin="10DP"android:gravity="Center"android:text="Eject Dialog"android:textsize= "@dimen/activity_horizontal_margin"/ > <textview android:id= "@+id/text2" android:layout_width= " Match_parent "android:layout_height=" wrap_content "android:layout_margin= "10DP"  Android:gravity= "center" android:text=  "Play Custom Layout Dialog" android:textsize=  "@dimen/activity_horizontal_margin"/></ LINEARLAYOUT>            

Update_manage_dialog layout

<RelativelayoutXmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:background="#00FFFFFF" ><RelativelayoutAndroid:layout_width="250DP"android:layout_height="250DP"Android:layout_centerinparent="True"Android:background="@drawable/UPDATE_BG" ><TextViewAndroid:id="@+id/dialog_title"Android:layout_width="Match_parent"android:layout_height="Wrap_content"android:layout_margintop="30DP"android:gravity="Center"android:text="Friendly and helpful"Android:textsize="18sp"/><TextViewAndroid:id="@+id/dialog_content"Android:layout_width="Match_parent"android:layout_height="Wrap_content"android:layout_below="@+id/dialog_title"android:layout_margintop="10DP"android:layout_marginleft="30DP"android:layout_marginright="30DP"android:text="The principle is basic \ n Practice the truth."Android:textsize="14sp"/><LinearLayoutAndroid:layout_width="Match_parent"android:layout_height="Wrap_content"Android:layout_alignparentbottom="True"android:orientation="Horizontal" ><ButtonAndroid:id="@+id/dialog_btn_cancel"Android:layout_width="0DP"android:layout_height="Wrap_content"android:layout_weight="1"Android:background="@null"android:text=CancelAndroid:textcolor="#AAAAAA"Android:textsize="14sp"/><button android:id= @+id/dialog_ Btn_sure "android:layout_width=" 0DP " android:layout_height= "wrap_content" android:layout_weight= "1"  Android:background= "@null" android:text=  "update Now" android:textsize= "14SP"/ > </linearlayout> </relativelayout></RelativeLayout>  

UPDATE_BG put in drawable, the code is as follows

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- android:radius 弧形的半径 --> <corners android:radius="30dp" /> <!-- 填充的颜色 --> <solid android:color="@android:color/white" /></shape>

Go to Android alertdialog custom layout

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.