效果如下图

对话框布局

dialog_uninstallation_confirmation.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/uninstallation_dialog_background"
android:orientation="vertical"> <ImageView
android:id="@+id/iconImageView"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:layout_marginBottom="30dp"
tools:src="@mipmap/ic_launcher" /> <TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/black"
tools:text="是否卸载CustomDialog" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginBottom="15dp"> <TextView
android:id="@+id/cancelTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/uninstallation_dialog_button_background"
android:gravity="center"
android:padding="18dp"
android:text="取消"
android:textColor="@android:color/black" /> <TextView
android:id="@+id/uninstallTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:background="@drawable/uninstallation_dialog_button_background"
android:gravity="center"
android:padding="18dp"
android:text="卸载"
android:textColor="#FF0000" />
</LinearLayout>
</LinearLayout>

自定义的对话框类

UninstallationConfirmationDialog.java

package com.bu_ish.custom_dialog_example;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView; public abstract class UninstallationConfirmationDialog extends Dialog {
private ImageView iconImageView;
private TextView messageTextView;
private int iconResId;
private String message; public abstract void onCancelClicked(); public abstract void onUninstallClicked(); public UninstallationConfirmationDialog(Context context) {
super(context, R.style.UninstallationConfirmationDialog);
} public UninstallationConfirmationDialog setIcon(int resId) {
this.iconResId = resId;
return this;
} public UninstallationConfirmationDialog setMessage(String message) {
this.message = message;
return this;
} @Override
public void show() {
super.show();
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_uninstallation_confirmation);
iconImageView = findViewById(R.id.iconImageView);
messageTextView = findViewById(R.id.messageTextView);
iconImageView.setImageResource(iconResId);
messageTextView.setText(message);
findViewById(R.id.cancelTextView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCancelClicked();
dismiss();
}
});
findViewById(R.id.uninstallTextView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onUninstallClicked();
dismiss();
}
});
}
}

对话框style

    <style name="UninstallationConfirmationDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>

P.S.

对话框默认风格是带标题的,通过windowNoTitle设置为无标题

为保证对话框宽度与屏幕匹配,须通过Window.setAttributes(WindowManager.LayoutParams)设置宽度

完整Demo链接:https://pan.baidu.com/s/1RItjQZ7v1xMHL7b5C6lLzw,提取码:qbwu

Android笔记之自定义对话框的更多相关文章

  1. [android] 手机卫士自定义对话框布局

    手机防盗页面部分 点击手机防盗,进行判断,如果没有设置密码,显示一个设置密码的对话框,如果已经设置密码了,弹出输入密码对话框 密码保存在SharedPreferences中,数据取出进行判断 自定义一 ...

  2. Android开发之自定义对话框

    由于系统自带的对话框不好看,于是本人就自定义了一个对话框,以后有类似的就可以直接使用啦.先上效果图: 1,布局文件dialog_clear_normal.xml <?xml version=&q ...

  3. android笔记:DatePickerDialog日期设置对话框

    在开发中,可以通过DatePickerDialog来设置日期,TimePickerDialog来设置时间. 实例化DatePickerDialog对象之后,再调用show方法就可以显示对话框了. 具体 ...

  4. Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复

    效果图 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  5. Android笔记之自定义PopupWindow

    效果图 popup_window_addition.xml <?xml version="1.0" encoding="utf-8"?> <L ...

  6. Android开发学习笔记-自定义对话框

    系统默认的对话框只能显示简单的标题内容以及按钮,而如果想要多现实其他内容则就需要自定义对话框,下面是自定义对话框的方法. 1.先定义对话框的模版 <?xml version="1.0& ...

  7. Android笔记之为自定义对话框添加移动动画效果

    给底部的对话框添加移动动画效果 可通过Window.setWindowAnimations(int resId)设置 SharingDialog.java package com.bu_ish.sha ...

  8. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  9. Android中的AlertDialog使用示例五(自定义对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

随机推荐

  1. 转 C++ 面向对象程序设计的基本特点

    传送门 Miss it   C++ 面向对象程序设计的基本特点 First: 抽象 面向对象方法中的抽象,是指对具体问题(对象)进行概括,抽出一类对象公共性质并加以描述的过程. 抽象的过程,也是对问题 ...

  2. 小程序-引用其他js文件

    我也是小白菜,之所以有这个记录是因为我做项目时遇到了这个问题 流程: 1.需要建立一个js文件   content.js function myContent() { console.log(&quo ...

  3. 文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  4. Codeforces Gym - 101147G The Galactic Olympics

    Discription Altanie is a very large and strange country in Mars. People of Mars ages a lot. Some of ...

  5. Flink起步安装和使用

    下载安装 下载地址 下载对应操作系统和版本的flink  # 首先确认下Java环境 $ java -version  java version "1.8.0_111" Java( ...

  6. gradle_____最后到齐的构建工具

    从今年开始,开始换用gradle 了,个人感觉还好,配置不像maven,一堆xml 文件,一个jar 一行字符,内置的task 和很多.自定义task 也挺简单,比ant简单一些. 简单配置文件示例: ...

  7. Android View 布局流程(Layout)完全解析

    前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...

  8. 线性回归,logistic回归分类

    学习过程 下面是一个典型的机器学习的过程,首先给出一个输入数据,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计,也被称为构建一个模型.就如同上面的线性 ...

  9. Lazarus安装使用

    Lazarus安装使用 最后还是安装了Lazarus: 安装之后,新建了项目,还引入了Unit,就可以跑了: 学习:http://tieba.baidu.com/p/3164001113 progra ...

  10. 20. Spring Boot Servlet【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069482 Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可 ...