效果如下图

对话框布局

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++程序之反汇编

    #include<iostream> using namespace std; class point3d; class point2d; class point3d { private: ...

  2. Word Ladder系列

    1.Word Ladder 问题描述: 给两个word(beginWord和endWord)和一个字典word list,找出从beginWord到endWord之间的长度最长的一个序列,条件: 1. ...

  3. hdu 1728(搜索)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. 使用Python计算研究生学分绩(绩点)

    最近看了CSDN上一个专栏<Python爬虫入门教程>,其中最后一篇作者写了个例子,用爬虫计算山东大学绩点,顿时想到前一阵子搞测评的时候还得拿计算器一点点算自己的平均学分绩,也想写一个自己 ...

  5. AC日记——飞行员配对方案问题 洛谷 P2756

    题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...

  6. AC日记——太空飞行计划 洛谷 P2762

    题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的 ...

  7. (五)github删除仓库

    一.一直学习怎么创建仓库,创建了太多仓库,一直不知道咋删除,有点懵,其实很简单,就是对英文不太习惯,要加深英文水平. 找到setting,然后再下面找到danger Zone

  8. ubuntu系统克隆

    使用clonezilla,原文地址:http://www.linuxidc.com/Linux/2014-09/107117.htm 类似的一篇:http://storysky.blog.51cto. ...

  9. [ZJOI 2016] 小星星

    4455: [Zjoi2016]小星星 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 653  Solved: 400[Submit][Status] ...

  10. Windows下Python的虚拟环境

    前言 在开发python的应用程序的时候,有时候会遇到依赖包的版本问题,比如之前开发tensorflow应用的时候需要用到python3.5以下的版本(还是python3),但是日常做其它应用的时候用 ...