Custom dialog, android custom dialog

Source: Internet
Author: User

Custom dialog, android custom dialog

(1) You need to prepare the style of the custom dialog box, that is, a layout file.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ drawable/dialog_bg" android: gravity = "center_vertical | kernel" android: orientation = "vertical"> <RelativeLayout android: layout_width = "321dp" android: layout_height = "wrap_content" android: layout_weight = "7.45" android: paddingLeft = "30dip" android: paddingTop = "10dip"> <ImageView android: id = "@ + id/dialog_title_image" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: background = "@ drawable/dialog_title_image"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: layout_marginLeft = "10dip" android: layout_toRightOf = "@ id/dialog_title_image" android: text = "Title" android: textColor = "#000000" android: textSize = "30sp"/> </RelativeLayout> <TextView android: layout_width = "270dp" android: layout_height = "1dip" android: layout_marginTop = "5dip" android: background = "@ drawable/hide"/> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginLeft = "30dip" android: layout_marginTop = "10dip" android: text = "This is a custom dialog" android: textColor = "#000000"/> <RelativeLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "3.64" android: gravity = "bottom | center_horizontal" android: paddingBottom = "10dip" android: paddingTop = "10dip"> <Button android: id = "@ + id/dialog_button_cancel" android: layout_width = "100dip" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_marginLeft = "16dp" android: background = "@ drawable/btn_input_completed_normal" android: text = "cancel"/> <Button android: id = "@ + id/dialog_button_ OK" android: layout_width = "100dip" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/dialog_button_cancel" android: layout_alignBottom = "@ + id/dialog_button_cancel" android: layout_marginLeft = "48dp" android: layout_toRightOf = "@ + id/dialog_button_cancel" android: text = "OK"/> </RelativeLayout> </LinearLayout>

Layout file Image


(2) Write a custom dialog class to load the preceding layout file.

package com.example.zidingyidialog;import android.app.Dialog;import android.content.Context;import android.os.Bundle;public class mydialog extends Dialog {    Context context;    public mydialog(Context context) {        super(context);        // TODO Auto-generated constructor stub        this.context = context;    }    public mydialog(Context context, int theme){        super(context, theme);        this.context = context;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        this.setContentView(R.layout.dialog);    }}

(3) Next, write how to start the custom dialog box and how to listen to the buttons in the dialog box for response.

Package com. example. zidingyidialog; import android. app. activity; import android. app. dialog; import android. content. dialogInterface; import android. content. dialogInterface. onShowListener; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {/** Called when the activity is first created. */private Dialog dialog; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// initialize a custom Dialog dialog = new mydialog (MainActivity. this, R. style. myDialog); dialog. show (); Button button = (Button) dialog. findViewById (R. id. dialog_button_ OK); button. setOnClickListener (l); Button butto = (Button) Diener. findViewById (R. id. dialog_button_cancel); butto. setOnClickListener (l1) ;}}) ;}onclicklistener l = new OnClickListener () {@ Overridepublic void onClick (View v) {dialog. dismiss () ;}}; OnClickListener l1 = new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (MainActivity. this, "hhahha", Toast. LENGTH_SHORT ). show ();}};;}

Add the following content to styles under values to facilitate the control of the dialog box format.
</style>       <style name="MyDialog" parent="@android:Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowNoTitle">true</item>         <item name="android:windowBackground">@drawable/dialog_bg</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowContentOverlay">@null</item>    </style>

The above are the basic steps for adding a custom dialog box







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.