效果如下图

对话框布局

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. getID3类的学习使用

    getID3类的学习使用 网上描述: getID3()这个PHP脚本能够从MP3或其它媒体文件中提取有用的信息如:ID3标签,bitrate,播放时间等. (格式包括:Ogg,WMA,WMV,ASF, ...

  2. C语言集锦(二) 图像显示 Windows和Linux

    关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库. 了解最原始的方式,对 ...

  3. VUE之命令行报错:Expected indentation of 4 spaces but found 6

    使用vue时候,经常被一大片警告惊呆了,这是webpack默认的语法检查插件ESLint在做警告, [ESLint是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确.风格统一的代码] 但是我 ...

  4. 如何让一个现有的程序集运行在Silverlight环境中

    故事是这样的:我们有一个组件,是一个标准的Class Library,里面有一些代码是实现了某些计算或者业务逻辑.例如下面这样 然后,我们做了一个Silverlight的应用程序,和一个用于运行该程序 ...

  5. ML | PCA

    what's xxx PCA principal components analysis is for dimensionality reduction. 主要是通过对协方差矩阵Covariance ...

  6. vim可视化&Linux系统安全最小化原则& su & sudo

    一.vim在可视化模式下编辑 crl+v,会变成-- VISUAL BLOCK --,然后用上下左右键去选中. 多行注释: ESC进入命令行模式; Ctrl+v进入VISUAL BLOCK模式 上下左 ...

  7. SPOJ 8222 Substrings

    题面 Description 给长度为 n 的字符串 S , 对任意的 L , 求长度为 L 的子串最多出现的次数. Input String S consists of at most 250000 ...

  8. eclipse不会自动编译的问题解决

    注意:非必要的时候,重新下载eclipse安装是最有效的解决方法. 以下为尝试的步骤: 1.看看project->Build Automatically有没有勾上?如果没有,勾上以后,clean ...

  9. 邁向IT專家成功之路的三十則鐵律 鐵律九:IT人社群互動之道-縮小自己

    身為一位專業的IT人士所要學習的東西實在非常的多,然而對於時間相當有限的我們,最快速的學習方法就是向他人學習,而向他人學習的首要態度就是「縮小自己」.唯有將自己縮小到別人的眼睛裡,才能夠讓他們真心誠意 ...

  10. awk如何区分shell脚本传进来的参数和自身的参数?awk如何获取shell脚本传进来的参数;awk中如何执行shell命令

    问题:对于shell脚本,$0表示脚本本身,$1表示脚本的第一个参数,$2……依次类推:对于awk,$1表示分割后的第一个字段,$2……依次类推.那么对于shell脚本中的awk如何区分两者呢? 答案 ...