Android Custom Component Family "13"--android Customize dialog box so simple

Source: Internet
Author: User

dialog boxes are used in many areas of our daily projects, but the Android system provides us with a dialog that is very uncoordinated with our well-designed interface, in which case we want to be free to define the dialog box, or sometimes our dialog is a picture with no caption and buttons, For example, a series of requirements, this article let us show you how to customize our dialog box as you would with activity.

There are several ways to customize the dialog box in general:

1, rewrite dialog to achieve.

2. Get the Window object implementation of dialog.

3, using WindowManager to achieve.

4, using Dialogtheme to achieve.

Let's introduce the second and fourth ways, and put the encapsulated code out, the third way to face WindowManager after the introduction of the time to say.

Reprint please indicate source: Http://blog.csdn.net/dawanganban


One, custom dialog

Main interface layout:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Fill_ Parent "    android:layout_height=" fill_parent "    android:orientation=" vertical "><button     android: Id= "@+id/dialog_custom_button"    android:layout_width= "wrap_content"    android:layout_height= "wrap_content "    android:text=" custom dialog box "/><button     android:id=" @+id/dialog_activity_button "    android:layout_ Width= "Wrap_content"    android:layout_height= "wrap_content"    android:text= "Activity Dialog"/></ Linearlayout>
Use two buttons to pop up two different types of dialogs

public class Mainactivity extends Activity implements onclicklistener{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); FindViewById ( R.id.dialog_custom_button). Setonclicklistener (this); Findviewbyid (R.id.dialog_activity_button). Setonclicklistener (this);} @Overridepublic void OnClick (view view) {switch (View.getid ()) {case R.id.dialog_custom_button:dialogcustom dc = new Dial Ogcustom (Mainactivity.this, R.layout.dialog);d C.setdismissbuttonid (r.id.close_dialog); Break;case R.id.dialog_ Activity_button:intent Intent = new Intent (mainactivity.this, Dialogactivity.class); startactivity (Intent); break; Default:break;}}}
The methods for customizing dialog boxes are simple to encapsulate, easy to use, and can be encapsulated according to your project requirements.

Package Com.example.testcustomdialog;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.context;import Android.view.view;import Android.view.view.onclicklistener;import android.view.window;/** * Public Custom dialog box * @author Administrator * */public class Dialogcustom{private Android.app.AlertDialog.Builder builder;private int layout;private alertdialog dialog;private Window window;public Dialogcustom (context context, int layout) {builder = new Alertdialog.builder (context); this.layout = layout;} Public Dialogcustom (context context, int theme, int layout) {builder = new Alertdialog.builder (context, theme); this.layout = layout;} Public Builder Getbuilder () {return builder;} /** * Gets the Window object of the dialog box * @return */public window GetWindow () {dialog = Builder.create ();d ialog.show (); window = Dialog.getwin Dow (); Window.setcontentview (layout); return window;} /** * Gets the corresponding view * @param ID * @return */public view Getviewbyid (int id) {if (window = = null) GetWindow () and return window by ID.Findviewbyid (ID);} /** * Set the button ID to add a close event * @param id */public void setdismissbuttonid (int id) {View view = Getviewbyid (ID); view.setonclicklist Ener (New Onclicklistener () {@Overridepublic void OnClick (View v) {dismiss ();}});} /** * Close dialog box */public void Dismiss () {if (dialog! = null) {Dialog.dismiss ();}}}
Then create the dialog object to add a listening view, which works mainly in the layout file, which is the content of your dialog box.

<?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:orientation=" vertical "&    Gt <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:or ientation= "vertical" android:layout_gravity= "center_horizontal" android:gravity= "Center_horizontal" a Ndroid:paddingbottom= "30dip" android:background= "#ffffff" > <textview android:layout_width= "260dip" android:layout_height= "40dip" android:layout_margin= "30dip" android:gravity= "cen            ter "android:text=" This is Activitydialog "/> <button android:id=" @+id/close_dialog " Android:layout_width= "100dip" android:layout_height= "30dip" android:text= "click Close" Androi d:background= "@drawable/closE_butten_p "/> </LinearLayout></LinearLayout> 
Second, the use of Dialogtheme
    <style name= "Dialogactivitytheme" parent= "Android:style/theme.dialog" >        <item name= "Android: Windowbackground "> @android:color/transparent</item>        <item name=" Android:windowframe "> @null </item>        <item name= "Android:windownotitle" >true</item>        <item name= "Android: Windowisfloating ">true</item>        <item name=" Android:windowistranslucent ">true</item>        <item name= "Android:windowcontentoverlay" > @null </item>        <item name= "Android:width" >1px </item>        <item name= "android:height" >1px</item>        <item name= "Android: Windowanimationstyle "> @android:style/animation.dialog</item>        <item name=" Android: Backgrounddimenabled ">true</item>    </style>
Register Dialogactivity in Activitymanifest.xml

        <activity             android:name= "com.example.testcustomdialog.DialogActivity"            android:theme= "@style/ Dialogactivitytheme "            android:screenorientation=" Portrait "></activity>
So that we can create a dialog box just like normal activity, isn't it simple.

Summary of the three or two dialog boxes

A dialog box that looks like it is very different, and the dialog box created in the second way is actually an activity, so it has the life cycle and all the attributes of the activity. It affects the life cycle of other activity in the stack. And if there are input boxes that need to be entered in the dialog box, it is recommended to use this method to automatically bring up the input method and make the screen automatically adapt, and the first way of dialog box creation is particularly convenient, when you need to display a hint of information to be used. ( source code download : http://download.csdn.net/detail/lxq_xsyu/8315023)

CSDN Blog star activity began, vote for me:http://vote.blog.csdn.net/blogstar2014/details?username=lxq_xsyu#content


Android Custom Component Family "13"--android Customize dialog box so simple

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.