Android Custom View (1): dialog box-dialog

Source: Internet
Author: User
Tags border color

Android system comes with a dialog box, on many Android 5.0 under the system of mobile phones, can not bear to see, so the UI design basically need to customize the dialog box, beautiful dialog boxes, how to design a simple custom dialog box of Android.

One, dialog need to pay attention to the problem

Android Pop-up dialog must exist in the activity, not out of thin air, so dialog not in the application class inside new, must be after the activity oncreate new.

1, the default dialog
publicDialog(Context context) {        this0true);    }
2, Customizing dialog Box Style dialog
publicDialogint theme) {        thistrue);    }
Two, dialog instance Java code

The instance code is as follows:

  Public void ShowDialog() {if(dialog!=NULL&&dialog.isshowing ()) {return; } dialog =NewDialog (Mcontext, R.STYLE.DIALOG1);        Dialog.setcontentview (R.layout.dialog_common);        Button Btn_left = (button) Dialog.findviewbyid (r.id.btn_left);        Button Btn_right = (button) Dialog.findviewbyid (r.id.btn_right);        TextView tv_body_msg = (TextView) Dialog.findviewbyid (r.id.tv_body_msg);        Tv_body_msg.settext (R.STRING.DIALOG_MSG); Dialog.setcancelable (true);//Click the left buttonBtn_left.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {//todo OK}        });//Right-clickBtn_right.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {//todo Cancel}        });if(!mcontext.isfinishing ())        {dialog.show (); }    }
Three, Dialog style

Sets the border, transparency, background, and other style of the dialog box.

<!--Customize dialog box--<stylename="Dialog1"Parent="@android: Style/theme.dialog"> <Item name="Android:windowframe"> @null </Item> <!--border--<Item name="Android:windowisfloating">true</Item> <!--whether it appears on the activity--<Item name="Android:windowistranslucent">false</Item> <!--Translucent--<Item name="Android:windownotitle">true</Item> <!--Untitled--<Item name="Android:windowbackground"> @android: color/transparent</Item> <!--Clear background --<Item name="Android:backgrounddimenabled">false</Item> <!--Blur--</style>
Four, Shape shapes

The main point is to achieve the fillet effect of the dialog, dialog the outer corner is defined with shape.

<?xml version= "1.0" encoding= "UTF-8"?><shape xmlns:android="Http://schemas.android.com/apk/res/android"  Android:shape="Rectangle" >        <!--fill color: Here set the background transparent--   <!--<solid android:color= "@android: Color/transparent"/> --    <solid android:color="#FFF" />    <!--border color: cannot be the same as the window background color--    <strokeandroid:width="5DP"android:color="#ffffff" / >                    <!--set the four corners of the button arc--    <!--Android:radius arc radius--    <corners Android:radius="5dip" /></shape>
Five, dialog box body XML layout

Define Title,body text, left and right buttons, and so on.

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:background  =" @drawable/dialog_shape_bg " android:layout_margin  = "10DP"  android:padding  =" 0DP "  Android:orientation  =;     <TextViewandroid:id= "@+id/tv_title"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:text=" Hint " Android:textsize="20sp"android:textcolor="#666666"android:layout_ MarginTop="10DP"android:layout_gravity="center" />                                                                    <TextView        Android:id="@+id/tv_body_msg"        Android:layout_width="Wrap_content"        Android:layout_height="Wrap_content"        Android:minwidth="250DP"        Android:text="@string/dialog_wifi_msg"        android:textsize="18SP"        Android:paddingleft="20DP"        Android:paddingright="20DP"        Android:textcolor="#666666"        Android:layout_margintop="10DP"        android:gravity="Center"/>    <view  android:layout_ Width  = "match_parent"  android:layout_height< /span>= "0.5DP"  android:layout_margintop  =< Span class= "Hljs-value" > "15DP"  android:background  =" #C2C2C2 "/>     <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Wrap_content"android:layout_margintop="0DP"android:orientation=  "Horizontal">                                        <Button            Android:id="@+id/btn_left"            Android:layout_width="0DP"            Android:layout_height="40DP"           style="Android:attr/buttonbarbuttonstyle"            Android:text="@string/dialog_btn1"            android:textsize="18SP"                      Android:background="@android: Color/transparent"            Android:textcolor="#006600"            Android:layout_weight="1"/>        <view  android:layout_wid th  = "0.5dip"  android:layout_height  = "match_parent"  android:background  = "#C2C2C2"  android:layout_gravity  =" center_horizontal "/>         <button  android:id= "@+id/btn_right"  android:layout_width  =
     
       "0DP" 
      android:layout_height  =" 40DP " style  =" Android:attr/buttonbar ButtonStyle " android:text  =" @string/dialog_b Tn2 " android:background  =" @android: color/ Transparent " android:textcolor  =" #006600 " android:layout_weight  = "1" /     </linearlayout></linearlayout>
Summarize

Android custom dialog, the main difference is the main object of the XML layout, which can be implemented in the view to implement some event operations, such as input text, determine, cancel and so on. A lot of practice, or very simple.
To be continued, Du Hu, Dusan,q 291902259.

Android Custom View (1): dialog box-dialog

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.