详解Android中Dialog的使用
发布时间 - 2026-01-10 22:16:51 点击率:次在Android中经常要使用Dialog来实现一些提示以及一些特殊的效果,而且样式也不一样,每次都得查一大堆资料,还不一定能解决,这里总结一些常用的Dialog的实践。

普通的Dialog
//普通的AlertDialog对话框
findViewById(R.id.btn_common).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("普通的对话框的标题");
builder.setMessage("这是一个普通的对话框的内容");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toast("取消");
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toast("确定");
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
这里使用AlertDialog来显示一个系统的提示对话框,效果如下:
修改普通对话框的位置、大小、透明度
主要是在普通的dialog.show() 下面加上如下代码
//放在show()之后,不然有些属性是没有效果的,比如height和width Window dialogWindow = dialog.getWindow(); WindowManager m = getWindowManager(); Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用 WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值 //设置高度和宽度 p.height = (int) (d.getHeight() * 0.4); // 高度设置为屏幕的0.6 p.width = (int) (d.getWidth() * 0.6); // 宽度设置为屏幕的0.65 //设置位置 p.gravity = Gravity.BOTTOM; //设置透明度 p.alpha = 0.5f; dialogWindow.setAttributes(p);
在这里,设置dialog的高为屏幕的高度的4/10,宽为屏幕宽带的6/10,同事位置为底部,透明度为半透明。当然还有很多其他属性,这里暂不介绍,你们可以自己试一试。效果如下:
使用普通的dialog来添加自定义布局
我们需自定义一个布局,如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:background="#00ff00"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#ff0000" android:text="你好"/> </LinearLayout>
我们这里新建了一个布局设置高度和宽度为100dp,线性布局里面包裹了一个TextView,布局很简单,当然也可以自定义一个复杂的布局,这里就不介绍了。来看看Java代码的实现。
// 使用普通的dialog来添加自定义布局
findViewById(R.id.btn_custom2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(R.layout.dialog_custom1);
AlertDialog dialog = builder.create();
dialog.show();
}
});
我们直接把我们的布局通过builder设置进去,看看效果:
这里的Dialog非常丑,这是与AlertDialog的默认主题有关,下面我们通过自定义主题来改变对话框的样式来使对话框变得漂亮。
在values/styles.xml自定义样式继承Android:Theme.Dialog来实现自己的样式
<style name="MyCommonDialog" parent="android:Theme.Dialog"> <!-- 背景颜色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 是否没有标题 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否背景模糊 --> <item name="android:backgroundDimEnabled">false</item> <!-- 设置背景模糊的透明度--> <item name="android:backgroundDimAmount">0.5</item> </style>
这里样式的属性都有注释,没种样式不是必须的,你可以自己试着改变一些值来查看效果以便达到自己的最佳效果。
在创建dialog的时候将样式传过去
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.MyCommonDialog);
现在的效果如下:
可以看的我们的布局的高度和宽带还是没效果,我们知道子空间的布局一般由布局来测量的于是我想到给这个布局的最外层套一个布局,看能不能达到我们的效果。
修改dialog_custom1.xml布局如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="#00ff00"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="你好" android:textColor="#ff0000"/> </LinearLayout> </RelativeLayout>
再次运行如下:
达到我们想要的效果了,这样你就可以引入样式和自定义布局实现各种对话框的效果了。
继承Dialog来实现Dialog
通过继承Dialog来实现自定义的Dialog,这样我们就可以在任何地方直接new我们的Dialog就可以实现特定的对话框了。
1.在values/styles.xml新建一个样式MyDialog
<style name="MyDialog" parent="android:Theme.Dialog"> <!-- 背景颜色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 是否没有标题 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否背景模糊 --> <item name="android:backgroundDimEnabled">false</item> <!-- 设置背景模糊的透明度--> <item name="android:backgroundDimAmount">0.5</item> </style>
2.新建一个MyDialog继承Dialog
public class MyDialog extends Dialog {
//在构造方法里预加载我们的样式,这样就不用每次创建都指定样式了
public MyDialog(Context context) {
this(context, R.style.MyDialog);
}
public MyDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 预先设置Dialog的一些属性
Window dialogWindow = getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes();
p.x = 0;
p.y = 100;
p.gravity = Gravity.LEFT | Gravity.TOP;
dialogWindow.setAttributes(p);
}
}
/*
* lp.x与lp.y表示相对于原始位置的偏移.
* 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
* 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
* 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
* 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
* 当参数值包含Gravity.CENTER_HORIZONTAL时,对话框水平居中,所以lp.x就表示在水平居中的位置移动
* lp.x像素,正值向右移动,负值向左移动.
* 当参数值包含Gravity.CENTER_VERTICAL时,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像
* 素,正值向右移动,负值向左移动.
* gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL
*/
这里对window的一些参数进行了解释,我把对话框设置的离左上角离顶部100px的位置。
3.使用MyDialog
自定义布局dialog_custom2.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="#ffffff" android:orientation="vertical" android:padding="10dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> </LinearLayout> </RelativeLayout>
Java代码
//继承Dialog来实现Dialog
findViewById(R.id.btn_custom3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog dialog = new MyDialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_custom2);
dialog.show();
}
});
4.查看效果:
给Dialog设置动画
1.新建动画文件
进入动画dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fillAfter="true" android:fromYDelta="100%p" android:toYDelta="0%"/> </set>
退出动画dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fillAfter="true" android:fromYDelta="0%" android:toYDelta="100%p"/> </set>
2.在values/styles.xml中新建样式
<style name="MyAnimDialog" parent="android:Theme.Dialog"> <!-- 背景颜色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 是否没有标题 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否背景模糊 --> <item name="android:backgroundDimEnabled">true</item> <!-- 设置背景模糊的透明度--> <item name="android:backgroundDimAmount">0.5</item> <!-- 动画 --> <item name="android:windowAnimationStyle">@style/dialog_animation</item> </style> <!-- 对话框显示和退出动画 --> <style name="dialog_animation"> <item name="android:windowEnterAnimation">@anim/dialog_enter</item> <item name="android:windowExitAnimation">@anim/dialog_exit</item> </style>
主要是给android:windowAnimationStyle指定我们新建的动画即可,引用和前面一样,这里就给出了,
3.查看效果
继承Dialog来实现底部弹出Dialog
自定义MyBottomDialog
public class MyBottomDialog extends Dialog {
public MyBottomDialog(Context context) {
this(context, R.style.MyAnimDialog);
}
public MyBottomDialog(Context context, int themeResId) {
super(context, themeResId);
//加载布局并给布局的控件设置点击事件
View contentView = getLayoutInflater().inflate(R.layout.dialog_custom3, null);
contentView.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "你好", Toast.LENGTH_SHORT).show();
}
});
super.setContentView(contentView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 预先设置Dialog的一些属性
Window dialogWindow = getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes();
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
getWindow().setAttributes(p);
p.height = (int) (d.getHeight() * 0.6);
p.width = d.getWidth();
p.gravity = Gravity.LEFT | Gravity.BOTTOM;
dialogWindow.setAttributes(p);
}
}
在onCreate方法里指定的Dialog的高度和宽度
布局dialog_custom3.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="#ffffff" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#00ff00" android:gravity="center" android:padding="10dp" android:text="你好" android:textColor="#000000"/> </LinearLayout> </RelativeLayout>
使用是方法是一样的
//继承Dialog来实现底部弹出Dialog
findViewById(R.id.btn_custom5).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyBottomDialog dialog = new MyBottomDialog(MainActivity.this);
dialog.show();
}
});
这里就不用设置布局了,因为我们在MyBottomDialog的构造方法里已经预加载了布局并设置了点击事件
查看效果:
自定义仿Meun的弹出Dialog
MyMenuDialog的代码
public class MyMenuDialog extends Dialog {
public MyMenuDialog(Context context) {
this(context, R.style.MyDialog);
}
public MyMenuDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 预先设置Dialog的一些属性
Window dialogWindow = getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//获取屏幕的高度和宽带
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
d.getMetrics(outMetrics);
//设置WindowManager.LayoutParams
// p.height = (int) (outMetrics.heightPixels * 0.6);
// p.width = (int) (outMetrics.widthPixels * 0.4);
//根据s随意来的高度来设置x轴偏移量
p.x = (int) (15 * outMetrics.density);
//根据Title的高度来设置y轴偏移量
p.y = (int) (45 * outMetrics.density);
p.gravity = Gravity.RIGHT | Gravity.TOP;
dialogWindow.setAttributes(p);
}
}
使用就不介绍了,这里主要是利用WindowManager.LayoutParams的x、y、gravity来实现的,当然可以自定义Dialog的弹出动画就可以实现一个菜单对话框了。
效果如下:
基本上Dialog的实现了这些效果应该能满足大部分项目的需求,至于以下复杂的,想带有ListView、GridView的Dialog等等都可以通过自定义Dialog来继承Dialog来实现,都是依葫芦画瓢就可以了,以后遇到什么再来补充。
# android
# dialog使用
# android中dialog
# dialog
# 自定义
# android控件封装 自己封装的dialog控件
# Android中自定义对话框(Dialog)的实例代码
# android dialog自定义实例详解
# Android中Dialog去黑边的方法
# Android开发笔记之:Dialog的使用详解
# Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
# Android 去掉自定义dialog的白色边框的简单方法
# Android修改源码解决Alertdialog触摸对话框边缘消失的问题
# Android Dialog 设置字体大小的具体方法
# Android入门之AlertDialog用法实例分析
# Android自定义ProgressDialog进度等待框
# 实例详解Android自定义ProgressDialog进度条对话框的实现
# 浅析Android中强大的Dialog
# 对话框
# 来实现
# 你好
# 弹出
# 出现在
# 就可以
# 自己的
# 这是
# 就不
# 加载
# 设置为
# 新建一个
# 主要是
# 都是
# 依葫芦画瓢
# 也不
# 是在
# 都有
# 在这里
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何实现API资源集合?(Resource Collection教程)
手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?
Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置
javascript中对象的定义、使用以及对象和原型链操作小结
如何用PHP快速搭建CMS系统?
Laravel如何创建自定义Facades?(详细步骤)
齐河建站公司:营销型网站建设与SEO优化双核驱动策略
Laravel如何处理文件上传_Laravel Storage门面实现文件存储与管理
网站制作价目表怎么做,珍爱网婚介费用多少?
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
html5如何实现懒加载图片_ intersectionobserver api用法【教程】
HTML5空格在Angular项目里怎么处理_Angular中空格的渲染问题【详解】
成都网站制作公司哪家好,四川省职工服务网是做什么用?
品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?
如何用5美元大硬盘VPS安全高效搭建个人网站?
微信小程序 canvas开发实例及注意事项
如何在宝塔面板中创建新站点?
如何在IIS7上新建站点并设置安全权限?
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
免费网站制作appp,免费制作app哪个平台好?
在Oracle关闭情况下如何修改spfile的参数
图册素材网站设计制作软件,图册的导出方式有几种?
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
如何快速重置建站主机并恢复默认配置?
ai格式如何转html_将AI设计稿转换为HTML页面流程【页面】
如何使用 Go 正则表达式精准提取括号内首个纯字母标识符(忽略数字与嵌套)
原生JS实现图片轮播切换效果
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
为什么要用作用域操作符_php中访问类常量与静态属性的优势【解答】
郑州企业网站制作公司,郑州招聘网站有哪些?
Win11应用商店下载慢怎么办 Win11更改DNS提速下载【修复】
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】
儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?
如何快速选择适合个人网站的云服务器配置?
Laravel API路由如何设计_Laravel构建RESTful API的路由最佳实践
Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】
如何解决hover在ie6中的兼容性问题
javascript读取文本节点方法小结
如何快速打造个性化非模板自助建站?
Laravel如何实现用户注册和登录?(Auth脚手架指南)
Swift中switch语句区间和元组模式匹配
微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】
Laravel怎么实现验证码功能_Laravel集成验证码库防止机器人注册
网站制作企业,网站的banner和导航栏是指什么?
如何在 Go 中优雅地映射具有动态字段的 JSON 对象到结构体
Laravel如何实现本地化和多语言支持?(i18n教程)
详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
高端云建站费用究竟需要多少预算?
上一篇: ,都有哪些好的动漫网站?
上一篇: ,都有哪些好的动漫网站?

