博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android自定义Dialog简单实例
阅读量:6910 次
发布时间:2019-06-27

本文共 2279 字,大约阅读时间需要 7 分钟。

做Android应用中,最缺少不了的就是自定义Dialog,对于系统默认提供的Dialog样式,一般都不复合我们应用的样式。

自定义Dialog需要3步骤即可:
1、主要的重写Dialog的Java类
2、自定义布局文件、并设置Dialog Theme,在style.xml文件中加一个即可
3、使用方法

一、创建CustomPopDialog2.java类

import android.app.Dialog;import android.content.Context;import android.graphics.Bitmap;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager.LayoutParams;import android.widget.ImageView;/** * 该自定义Dialog应用在:弹出框居中显示图片,点击其他区域自动关闭Dialog * * @author SHANHY(365384722@QQ.COM) * @date   2015年12月4日 */public class CustomPopDialog2 extends Dialog {
public CustomPopDialog2(Context context) { super(context); } public CustomPopDialog2(Context context, int theme) { super(context, theme); } public static class Builder {
private Context context; private Bitmap image; public Builder(Context context) { this.context = context; } public Bitmap getImage() { return image; } public void setImage(Bitmap image) { this.image = image; } public CustomPopDialog2 create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final CustomPopDialog2 dialog = new CustomPopDialog2(context,R.style.Dialog); View layout = inflater.inflate(R.layout.dialog_share_qrcode, null); dialog.addContentView(layout, new LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT , android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); dialog.setContentView(layout); ImageView img = (ImageView)layout.findViewById(R.id.img_qrcode); img.setImageBitmap(getImage()); return dialog; } }}

这里简单说明下,我们自定义Dialog需要准备一个自己的View布局文件,主要关注create()方法即可,本例中就是直接显示一个图片。

二、自定义View的布局文件、并在style.xml中添加theme

三、使用自定义的Dialog

Bitmap bitmap = xxxxx;// 这里是获取图片Bitmap,也可以传入其他参数到Dialog中        CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context);        dialogBuild.setImage(bitmap);        CustomPopDialog2 dialog = dialogBuild.create();        dialog.setCanceledOnTouchOutside(true);// 点击外部区域关闭        dialog.show();

最终效果图:

效果图

你可能感兴趣的文章
修改Hosts文件
查看>>
Python基本概念
查看>>
网络工程师该何去何从
查看>>
TeXworks代码补全功能
查看>>
java+jsp+mysql网页制作总结(1)
查看>>
获取当前操作的IFrame 对象的方法
查看>>
年月日下拉选择三级联动(闰年判断),时间获取方法总结,特殊:获取当前月天数...
查看>>
Tallest Cow(POJ3263)
查看>>
POJ—Building a Space Station
查看>>
杭电oj Problem-1013 Digital Roots
查看>>
CRM 2013 切换显示语言
查看>>
Codeforces Round #544 (Div. 3) C. Balanced Team
查看>>
UML-对象图
查看>>
【leetcode】1037. Valid Boomerang
查看>>
一起学Android之Layout
查看>>
PHP网页计时工具——SESSION问题
查看>>
PHP 真正多线程的使用
查看>>
ip黑白名单防火墙frdev的原理与实现
查看>>
ajax全接触
查看>>
ps查看内存占用排序
查看>>