效果如下图

对话框布局

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. Opus 和 AAC 声音编码格式

    Opus编码器 是一个有损声音编码的格式,由互联网工程任务组(IETF)近来开发,适用于网络上的实时声音传输,标准格式为RFC 6716.Opus 格式是一个开放格式,使用上没有任何专利或限制. Op ...

  2. Install Battery Historian

    1. Recommended extra packages for Trusty 14.04 $ sudo apt-get update $ sudo apt-get install \ linux- ...

  3. [专题总结]数位DP

    总结: 1:第i个数符合要求了,所以接下来的数都可以.如果没限制, 那么是有  10i-1  个.如果有限制,那么是   (nowx % 10i-1)+1  . 2:两种状态设置 有设状态d      ...

  4. HDU 4405: Aeroplane chess

    类型:概率DP 题意:一条直线下飞行棋,色子六个面等概率.同时存在一些飞机航线,到了某个点可以直接飞到后面的另一个点,可以连飞,保证一个点至多一条航线.求到达或者超过终点 所需要 掷色子的期望次数. ...

  5. 本地hosts文件

    (1)什么是Hosts文件? Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网 ...

  6. AC日记——【模板】二分图匹配 洛谷 P3386

    题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...

  7. js-禁止微信内置浏览器调整字体大小

    js方法: (function(){      if(typeof WeixinJSBridge=="object"&& typeof WeixinJSBridge ...

  8. Codeforces Gym101502 H.Eyad and Math-换底公式

    H. Eyad and Math   time limit per test 2.0 s memory limit per test 256 MB input standard input outpu ...

  9. ListView 在设备切换横竖屏时保存状态

    比如listview在设备切换横竖屏时,仍然需要保证position, activity - > onSaveInstanceState  - > restoreInstanceState ...

  10. 【面试】最容易被问到的N种排序算法!

    面试官:小明,是吧?你都知道哪些排序算法,哪几种是稳定排序? 小明:这个我有总结! 关于排序稳定性的定义 通俗地讲就是能保证排序前两个相等的数其在序列的前后位置顺序和排序后它们两个的前后位置顺序相同. ...