Android 提供了AlertDialog类可通过其内部类Builder轻松创建对话框窗口,但是没法对这个对话框窗口进行定制,为了修改 AlertDialog 窗口显示的外观,解决的办法就是创建一个指定的 AlertDialog 和 AlertDialog.Builder 类。

定义外观

我们希望将上面默认的对话框外观修改为如下图所示的新对话框风格:

)_第1张图片" title="Custom Android Dialog" height="142" width="293" style="padding:0px; margin:0px; border:0px; max-width:600px;border:1px solid black;">

该对话框将支持下面特性:

  1. 可从资源或者字符串直接指定对话框标题
  2. 可从资源、字符串和自定义布局来设置对话框内容
  3. 可设置按钮和相应的事件处理

编写布局、样式和主题

该对话框使用一个定制的布局来输出内容,布局定义的id将用于访问标题 TextView,下面是定义文件:

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03 android:orientation="vertical"
04 android:layout_width="fill_parent"
05 android:minWidth="280dip"
06 android:layout_height="wrap_content">
07
08 <LinearLayout
09 android:orientation="vertical"
10 android:background="@drawable/header"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content">
13
14 <TextView
15 style="@style/DialogText.Title"
16 android:id="@+id/title"
17 android:paddingRight="8dip"
18 android:paddingLeft="8dip"
19 android:background="@drawable/title"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"/>
22
23 </LinearLayout>
24
25 <LinearLayout
26 android:id="@+id/content"
27 android:orientation="vertical"
28 android:background="@drawable/center"
29 android:layout_width="fill_parent"
30 android:layout_height="wrap_content">
31
32 <TextView
33 style="@style/DialogText"
34 android:id="@+id/message"
35 android:padding="5dip"
36 android:layout_width="fill_parent"
37 android:layout_height="wrap_content"/>
38
39 </LinearLayout>
40
41 <LinearLayout
42 android:orientation="horizontal"
43 android:background="@drawable/footer"
44 android:layout_width="fill_parent"
45 android:layout_height="wrap_content">
46
47 <Button
48 android:id="@+id/positiveButton"
49 android:layout_marginTop="3dip"
50 android:layout_width="0dip"
51 android:layout_weight="1"
52 android:layout_height="wrap_content"
53 android:singleLine="true"/>
54
55 <Button
56 android:id="@+id/negativeButton"
57 android:layout_marginTop="3dip"
58 android:layout_width="0dip"
59 android:layout_weight="1"
60 android:layout_height="wrap_content"
61 android:singleLine="true"/>
62
63 </LinearLayout>
64
65 </LinearLayout>

根节点 LinearLayout 的宽度设置为fill_parent而最小的宽度是280dip,因此对话框的宽度将始终为屏幕宽度的 87.5%

自定义的主题用于声明对话框是浮动的,而且使用自定义的背景和标题视图:

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <resources>
03
04 <stylename="Dialog"parent="android:style/Theme.Dialog">
05 <itemname="android:windowBackground">@null</item>
06 <itemname="android:windowNoTitle">true</item>
07 <itemname="android:windowIsFloating">true</item>
08 </style>
09
10 </resources>

接下来我们需要定义对话框的标题和消息的显示:

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <resources>
03
04 <stylename="DialogText">
05 <itemname="android:textColor">#FF000000</item>
06 <itemname="android:textSize">12sp</item>
07 </style>
08
09 <stylename="DialogText.Title">
10 <itemname="android:textSize">16sp</item>
11 <itemname="android:textStyle">bold</item>
12 </style>
13
14 </resources>

编写对话框和 Builder 类

最好我们要提供跟AletDialog.Builder类一样的方法:

001 packagenet.androgames.blog.sample.customdialog.dialog;
002
003 importnet.androgames.blog.sample.customdialog.R;
004 importandroid.app.Dialog;
005 importandroid.content.Context;
006 importandroid.content.DialogInterface;
007 importandroid.view.LayoutInflater;
008 importandroid.view.View;
009 importandroid.view.ViewGroup.LayoutParams;
010 importandroid.widget.Button;
011 importandroid.widget.LinearLayout;
012 importandroid.widget.TextView;
013
014 /**
015 *
016 * Create custom Dialog windows for your application
017 * Custom dialogs rely on custom layouts wich allow you to
018 * create and use your own look & feel.
019 *
020 * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
021 *
022 * <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a> antoine vianey
023 *
024 */
025 publicclassCustomDialogextendsDialog {
026
027 publicCustomDialog(Context context,inttheme) {
028 super(context, theme);
029 }
030
031 publicCustomDialog(Context context) {
032 super(context);
033 }
034
035 /**
036 * Helper class for creating a custom dialog
037 */
038 publicstaticclassBuilder {
039
040 privateContext context;
041 privateString title;
042 privateString message;
043 privateString positiveButtonText;
044 privateString negativeButtonText;
045 privateView contentView;
046
047 privateDialogInterface.OnClickListener
048 positiveButtonClickListener,
049 negativeButtonClickListener;
050
051 publicBuilder(Context context) {
052 this.context = context;
053 }
054
055 /**
056 * Set the Dialog message from String
057 * @param title
058 * @return
059 */
060 publicBuilder setMessage(String message) {
061 this.message = message;
062 returnthis;
063 }
064
065 /**
066 * Set the Dialog message from resource
067 * @param title
068 * @return
069 */
070 publicBuilder setMessage(intmessage) {
071 this.message = (String) context.getText(message);
072 returnthis;
073 }
074
075 /**
076 * Set the Dialog title from resource
077 * @param title
078 * @return
079 */
080 publicBuilder setTitle(inttitle) {
081 this.title = (String) context.getText(title);
082 returnthis;
083 }
084
085 /**
086 * Set the Dialog title from String
087 * @param title
088 * @return
089 */
090 publicBuilder setTitle(String title) {
091 this.title = title;
092 returnthis;
093 }
094
095 /**
096 * Set a custom content view for the Dialog.

更多相关文章

  1. Android开发人员需要具备的知识(很全)!
  2. Android(安卓)WindowManager与窗口管理
  3. Android软件开发之盘点所有Dialog对话框大合集(一)
  4. Android(安卓)热敏打印机打印二维码
  5. 不仅是微软和诺基亚,谁都无法 fork Android,因为它就没法 fork
  6. Cocos2d-x从C++端调用Android端的非静态函数接口
  7. Android:微软的金钱机器(更新)
  8. Android系统信息获取 之十四:获取WIFI热点相关信息
  9. Android(安卓)获取手机存储信息详解(内存,外存等)

随机推荐

  1. Android Settings 声音设置
  2. cocos2d-x在Android的运行流程始末
  3. 使用ThinDownloadManager下载apk以及noti
  4. android 2.3 wifi (一)
  5. 《Android Dev Guide》系列教程3:应用基
  6. Android6.0系统悬浮窗权限的问题解决方法
  7. 引用第三方进行Android前端与web后台的数
  8. 头像图片任意截取
  9. appium系列文章之(一)appium安装
  10. 关于Chronometer(计时器)暂停的问题