转自:http://www.itivy.com/android/archive/2011/11/3/android-dialog-customize.html

很多时候,可能Android默认的几种Dialog对话框已经不能满足我们的需求,我们需要自定义自己的Dialog对话框,包括样式的改变,功能的改变等等。今天,我给出一个Android自定义Dialog的例子,大家可以看看,如果这个自定义dialog的方法能够用得上,那我们完全可以定义出非常富有个性的dialog对话框了,先看一个自定义的dialog对话框效果图吧,很简单,只有一个Activity,当点击Button的时候就弹出这个自定义的Dialog

里面的几张图都比较丑,我不多会美工,随便用powerpoint画了几张图,原理是一样的,先不计较这些。下面正入正题

为了照顾到所有的码农,在些把所有的代码都贴出来

新建工程在此就不贴出来了,只是为了方便大家的复制粘贴,取包名为com.and.mydialog,主Activity取名为MyDialogActivity

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package com.and.mydialog;   import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; 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 MyDialogActivity extends Activity {      /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {          super .onCreate(savedInstanceState);          setContentView(R.layout.main);          Button button = (Button) findViewById(R.id.button1);          button.setOnClickListener( new OnClickListener() {                @Override              public void onClick(View v) {                                    //初始化一个自定义的Dialog                  Dialog dialog = new MyDialog(MyDialogActivity. this ,                          R.style.MyDialog);                    dialog.show();              }          });        } }
主布局文件main.xml
1 2 3 4 5 6 7 8 9 10 <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"      android:orientation = "vertical" android:layout_width = "fill_parent"      android:layout_height = "fill_parent" >      < Button          android:text = "显示自定义Dialog"          android:id = "@+id/button1"          android:layout_height = "wrap_content"          android:layout_width = "fill_parent" /> LinearLayout >
新建一个自定义的Dialog类,取名MyDialog,继承自Dialog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.and.mydialog;   import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View;   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);      }   }
相应的布局文件dialog.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"      android:orientation = "vertical"      android:layout_width = "wrap_content"      android:layout_height = "wrap_content"      android:gravity = "center_vertical|center_horizontal"      android:background = "@drawable/dialog_bg" >      < RelativeLayout          android:layout_width = "fill_parent"          android:layout_height = "wrap_content"          android:paddingLeft = "30dip"          android:paddingTop = "10dip" >          < ImageView              android:id = "@+id/dialog_title_image"              android:layout_alignParentLeft = "true"              android:layout_width = "wrap_content"              android:layout_height = "wrap_content"              android:background = "@drawable/dialog_title_image" />          < TextView              android:layout_width = "wrap_content"              android:layout_height = "wrap_content"              android:layout_marginLeft = "10dip"              android:layout_centerInParent = "true"              android:text = "Title"              android:layout_toRightOf = "@id/dialog_title_image"              android:textColor = "#000000"              android:textSize = "30sp" />                RelativeLayout >      < TextView              android:layout_width = "fill_parent"              android:layout_height = "1dip"              android:background = "@drawable/lins"              android:layout_marginTop = "5dip" />      < TextView          android:layout_width = "fill_parent"          android:layout_height = "wrap_content"          android:text = "This is a custom dialog"          android:textColor = "#000000"          android:layout_marginTop = "10dip"          android:layout_marginLeft = "30dip" />      < RelativeLayout          android:layout_width = "fill_parent"          android:layout_height = "wrap_content"          android:paddingTop = "10dip"          android:gravity = "bottom|center_horizontal"          android:paddingBottom = "10dip" >          < Button              android:id = "@+id/dialog_button_cancel"              android:layout_alignParentLeft = "true"              android:layout_width = "100dip"              android:layout_height = "wrap_content"              android:text = "确定" />          < Button              android:id = "@+id/dialog_button_ok"              android:layout_width = "100dip"              android:layout_height = "wrap_content"              android:layout_toRightOf = "@id/dialog_button_cancel"              android:layout_marginLeft = "35dip"              android:text = "取消" />      RelativeLayout > LinearLayout >

最主要的,是自定义的Style,我们自定义一个式样,用来改变默认的Dialog样式

在values文件夹下新建一个styles.xml文件

1 2 3 4 5 6 7 8 9 10 <? xml version = "1.0" encoding = "utf-8" ?> < resources >      < 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 > resources >
这样应该就OK了,为了方便大家测试本示例,在此一并附上不怎么好看的素材


好了,这样我们的自定义dialog就基本完成了,虽然是难看了点,但是基本思路就是这样了,美工好的同学可以用这个思路去写一个比较漂亮的android自定义dialog对话框。


更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 根据文件名称修改安卓默认的蓝牙接收文件地址
  4. Android(安卓)XML解析
  5. 赵雅智_AndroidManifest.xml配置文件详解
  6. 【Android】Android(安卓)SDK下载和更新失败的解决方法!!!
  7. 探究Android之ClassLoader
  8. Activity学习日记(一)
  9. Android中改变Activity的不同icon:activity-alias

随机推荐

  1. 在CSDN下载资源,扣了积分,下载的是一个404
  2. 孩子放在父母的兄弟姐妹后面。
  3. HTML5 Web Sockets与代理服务器交互
  4. HTML/CSS: 标签CSS规则w/伪类呈现不一致
  5. HTML5 的应用程序缓存和优势
  6. 通过html5 touch事件封装手势识别组件
  7. 关于大背景图片随浏览器百分比缩放的问题
  8. 关于 jq/js获取几层/多层frame/frameset
  9. 如果鼠标在图片上,如何在图片上获取文字?
  10. Html--树莓派作为Web服务器