Android Dialog(对话框)

一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功能.


  Android API 支持下列类型的对话框对象:
    警告对话框 AlertDialog:  一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型.


    进度对话框 ProgressDialog:  显示一个进度环或者一个进度条. 由于它是AlertDialog的扩展, 所以它也支持按钮.


    日期选择对话框 DatePickerDialog:  让用户选择一个日期.


    时间选择对话框 TimePickerDialog:  让用户选择一个时间.


  如果你希望自定义你的对话框, 可以扩展Dialog类.

 

  

做Android应用中,最缺少不了的就是自定义Dialog,对于系统默认提供的Dialog样式,一般都不复合我们应用的样式。 
自定义Dialog需要3步骤即可: 
1、主要的重写Dialog的Java类 
2、自定义布局文件、并设置Dialog Theme,在style.xml文件中加一个即可 
3、使用方法

 

一、创建CustomPopDialog2.java类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:gravity="center"
    android:id="@+id/rootLayout">
 
    <ImageView
        android:id="@+id/img_qrcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="二维码" />
 
</LinearLayout>

  

1
2
3
4
5
6
<style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:background">#00000000</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

  三、使用自定义的Dialog

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

  

最终效果图:

效果图

 

posted @   mingruqi  阅读(398)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示