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;">
该对话框将支持下面特性:
- 可从资源或者字符串直接指定对话框标题
- 可从资源、字符串和自定义布局来设置对话框内容
- 可设置按钮和相应的事件处理
编写布局、样式和主题
该对话框使用一个定制的布局来输出内容,布局定义的id将用于访问标题 TextView,下面是定义文件:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns: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" > |
09 |
android:orientation = "vertical" |
10 |
android:background = "@drawable/header" |
11 |
android:layout_width = "fill_parent" |
12 |
android:layout_height = "wrap_content" > |
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" /> |
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" > |
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" /> |
42 |
android:orientation = "horizontal" |
43 |
android:background = "@drawable/footer" |
44 |
android:layout_width = "fill_parent" |
45 |
android:layout_height = "wrap_content" > |
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" /> |
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" /> |
根节点 LinearLayout 的宽度设置为fill_parent而最小的宽度是280dip,因此对话框的宽度将始终为屏幕宽度的 87.5%
自定义的主题用于声明对话框是浮动的,而且使用自定义的背景和标题视图:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
04 |
< style name = "Dialog" parent = "android:style/Theme.Dialog" > |
05 |
< item name = "android:windowBackground" >@null</ item > |
06 |
< item name = "android:windowNoTitle" >true</ item > |
07 |
< item name = "android:windowIsFloating" >true</ item > |
接下来我们需要定义对话框的标题和消息的显示:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
04 |
< style name = "DialogText" > |
05 |
< item name = "android:textColor" >#FF000000</ item > |
06 |
< item name = "android:textSize" >12sp</ item > |
09 |
< style name = "DialogText.Title" > |
10 |
< item name = "android:textSize" >16sp</ item > |
11 |
< item name = "android:textStyle" >bold</ item > |
编写对话框和 Builder 类
最好我们要提供跟AletDialog.Builder类一样的方法:
001 | package net.androgames.blog.sample.customdialog.dialog; |
003 | import net.androgames.blog.sample.customdialog.R; |
004 | import android.app.Dialog; |
005 | import android.content.Context; |
006 | import android.content.DialogInterface; |
007 | import android.view.LayoutInflater; |
008 | import android.view.View; |
009 | import android.view.ViewGroup.LayoutParams; |
010 | import android.widget.Button; |
011 | import android.widget.LinearLayout; |
012 | import android.widget.TextView; |
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. |
020 |
* Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html |
022 |
* <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a> antoine vianey |
025 | public class CustomDialog extends Dialog { |
027 |
public CustomDialog(Context context, int theme) { |
028 |
super (context, theme); |
031 |
public CustomDialog(Context context) { |
036 |
* Helper class for creating a custom dialog |
038 |
public static class Builder { |
040 |
private Context context; |
042 |
private String message; |
043 |
private String positiveButtonText; |
044 |
private String negativeButtonText; |
045 |
private View contentView; |
047 |
private DialogInterface.OnClickListener |
048 |
positiveButtonClickListener, |
049 |
negativeButtonClickListener; |
051 |
public Builder(Context context) { |
052 |
this .context = context; |
056 |
* Set the Dialog message from String |
060 |
public Builder setMessage(String message) { |
061 |
this .message = message; |
066 |
* Set the Dialog message from resource |
070 |
public Builder setMessage( int message) { |
071 |
this .message = (String) context.getText(message); |
076 |
* Set the Dialog title from resource |
080 |
public Builder setTitle( int title) { |
081 |
this .title = (String) context.getText(title); |
086 |
* Set the Dialog title from String |
090 |
public Builder setTitle(String title) { |
096 |
* Set a custom content view for the Dialog. |
- Android开发人员需要具备的知识(很全)!
- Android(安卓)WindowManager与窗口管理
- Android软件开发之盘点所有Dialog对话框大合集(一)
- Android(安卓)热敏打印机打印二维码
- 不仅是微软和诺基亚,谁都无法 fork Android,因为它就没法 fork
- Cocos2d-x从C++端调用Android端的非静态函数接口
- Android:微软的金钱机器(更新)
- Android系统信息获取 之十四:获取WIFI热点相关信息
- Android(安卓)获取手机存储信息详解(内存,外存等)
随机推荐
-
Android Settings 声音设置
-
cocos2d-x在Android的运行流程始末
-
使用ThinDownloadManager下载apk以及noti
-
android 2.3 wifi (一)
-
《Android Dev Guide》系列教程3:应用基
-
Android6.0系统悬浮窗权限的问题解决方法
-
引用第三方进行Android前端与web后台的数
-
头像图片任意截取
-
appium系列文章之(一)appium安装
-
关于Chronometer(计时器)暂停的问题