Android中自定义对话框(Dialog)的实例代码
发布时间:2022-01-19 06:10:38
1.修改系统默认的Dialog样式(风格、主题)
2.自定义Dialog布局文件
3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类
第一步:
我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了一个样式,
<style name="Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
<item name="android:windowBackground">@android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
我们可以看到,在Themes.xml中定义的Dialog的样式,其中,定义了window的标题样式,window的背景图,是否悬浮等等。
那么,我们要创建具有自定义样式的Dialog就可以创建一个styles.xml,在其中定义我们自己的Dialog样式,让其继承自Theme.Dialog样式,并修改其中的某些属性即可。
定义我们自己的Dialog样式:
a.创建一个styles.xml文件,放在res/values 文件夹下(当然了,这就不用说了。。。啰嗦一下)
b.在styles.xml中定义Dialog样式,代码如下:
<style name="Theme_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
上面代码中,定义了一个样式Theme_dialog,继承自@android:style/Theme.Dialog,然后定义了Dialog所在Window的背景图,此处使用的是透明颜色#00000000,然后定义了Dialog所在的Window隐藏标题(系统定义Dialog样式是带有标题的,在此设置此属性为true可隐藏标题),自定义Dialog样式到这就完成了。
第二步:
定义Dialog的布局:
创建一个布局文件layout_dialog.xml,代码如下:
<?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"
android:background="@drawable/pp_bg_dialog"
android:gravity="center">
<ProgressBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/progressbar_normal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
style="@style/text_white_small" android:layout_marginTop="5dp"
android:text="正在删除..." android:id="@+id/message"/>
</LinearLayout>
这里使用了一个垂直方向的线性布局,并且设置所有子元素居中,子元素为已个进度条ProgressBar和一个TextView。
此处,ProgressBar采用自定义样式,使用系统默认的ProgressBar可达到同样的效果(大同小异)。LinearLayout的背景
android:background="@drawable/pp_bg_dialog"(即上面效果图中居中显示的黑色透明背景框)是一个自定义的图片资源Shape:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#55000000"/>
</shape>
代码中定义了一个矩形,并设置了圆角和颜色。到此,Dialog的布局就完成了。
第三步:
自定义CustomDialog类,继承自Dialog,代码如下:
public class CustomDialog extends Dialog { 2
private static int default_width = 160; //默认宽度
private static int default_height = 120;//默认高度
public CustomDialog(Context context, int layout, int style) {
this(context, default_width, default_height, layout, style);
}
public CustomDialog(Context context, int width, int height, int layout, int style) {
super(context, style);12
//set content
setContentView(layout);
//set window params
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//set width,height by density and gravity
float density = getDensity(context);
params.width = (int) (width*density);
params.height = (int) (height*density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
private float getDensity(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.density;
}
}
在构造方法中设置了Dialog的contentView,并且设置了Window的宽度、高度和居中显示。
CustomDialog使用方法如下:
public class CustomDialogDemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
CustomDialog dialog1 = new CustomDialog(this, R.layout.common_dialog, R.style.Theme_dialog);//Dialog使用默认大小(160)
CustomDialog dialog2 = new CustomDialog(this, 180, 180, R.layout.common_dialog, R.style.Theme_dialog);
dialog2.show();//显示Dialog
//如果要修改Dialog中的某个View,比如把"正在删除..."改为"加载中..."
TextView mMessage = (TextView) dialog2.findViewById(R.id.message);
mMessage.setText("加载中...");
}
}
大体过程就是这样,根据以上大家可以自由发挥吧,希望都设计出自己满意的dialog。


猜你喜欢
- 头文件:#include<memory.h>1.memcpy作用:内存拷贝函数原型:void *memcpy( void *de
- graylog配置springboot配置依赖compile group: 'de.siegmar', name: '
- 枚举的定义1.题目枚举是JAVA 5.0后增加的一个重要类型。可以用来表示一组取值范围固定的变量。使用enum关键字,可以定义枚举类型。实现
- 前言已经有两个月没有更新博客了,其实这篇文章我早在两个月前就写好了,一直保存在草稿箱里没有发布出来。原因是有一些原理性的东西还没了解清楚,最
- 一、简述1、AOP的概念如果你用java做过后台开发,那么你一定知道AOP这个概念。如果不知道也无妨,套用百度百科的介绍,也能让你明白这玩意
- 一、前言 验证码可以说在我们生活中已经非常普遍了,任何一个网站,任何一个App都
- 一、排序与去重日常工作中,总会有一些场景需要对结果集进行一些过滤。比如,与第三方交互后获取的结果集,需要再次排序去重,此时就会根据某个字段来
- 微服务启动时报错2021-05-18 21:25:44.644 WARN 5452 — [tbeatExecutor-0
- Linux Hadoop 2.7.3 安装搭建Hadoop实现了一个分布式文件系统(Hadoop Distributed File Syst
- 深色主题工具类package com.example.kotlindemo.utilsimport android.content.Cont
- 一、项目简述环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,
- 作为开发人员,掌握开发环境下的调试技巧十分有必要。去年就想把关于Eclipse断点调试总结下了,由于对时间的掌控程度仍需极大提高,结果拖到今
- 对于某些程序,我们只允许它使用某些特定端口、网络类型或者特定IP类型等信息。这时候,需要使用到防火墙里面的“高级设置”,创建某些特定的入站或
- 现在看我文章的多数是一些老Android了,相信每个人使用起LayoutInflater都是家常便饭,信手拈来。但即使是这样,我仍然觉得这个
- Android可以在所有应用上方添加View,就是给WindowManager添加一个View,在创建的View的时候可以给这个View设置
- 什么是TaskScheduler?SynchronizationContext是对“调度程序(scheduler)&am
- 使用简单的fragment实现左侧导航,供大家参考,具体内容如下先上效果图:MainActivity.javapublic class Ma
- 作为一个WPF控件开发者,我在工作中经常遇到如本文标题所示的问题。其实,这个问题并不是很难,只是在操作上有些繁琐。本文将尝试对这个问题进行解
- 温馨提示:本教程的 GitHub 地址为「intellij-idea-tutorial」,欢迎感兴趣的童鞋Star、Fork,纠错。首先,给
- 从今天开始写关于C#的系列文章,本篇文章主要讲解C#中的委托使用。委托其实就是一种数据类型,和int,string是一样的概念。如果要把一个