Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog。

本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Builder类来设置Dialog的图标、标题、内容和按钮。

如果想要在Dialog中使用输入框或者其他控件,方法也是类似的,只要写好布局再加载就可以了。

Github:https://github.com/imcloudfloating/DesignApp

效果:

布局文件代码:

(注意这里的根布局的宽高如果用match_parent或者设置为具体的数值都和wrap_conten效果一样,可以通过设置子控件的大小来撑开)

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"> <LinearLayout
android:id="@+id/dialog_header"
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
android:background="@color/colorGreen"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"> <!-- Icon -->
<ImageView
android:contentDescription="@id/dialog_title"
android:id="@+id/dialog_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_check_circle" /> <!-- Title(default is gone) -->
<TextView
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:visibility="gone" /> </LinearLayout> <LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@+id/dialog_header"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"> <!-- Dialog Message -->
<TextView
android:id="@+id/dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
tools:text="Dialog Message" /> <Button
android:id="@+id/dialog_button"
android:layout_width="100dp"
android:layout_height="42dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:background="@drawable/bg_dialog_button"
android:textColor="#ffffff"
android:text="@string/dialog_button"> </Button> </LinearLayout> </android.support.constraint.ConstraintLayout>

InfoDialog类:

 package com.cloud.design.dialog;

 import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import com.cloud.design.R; public class InfoDialog extends Dialog { private InfoDialog(Context context, int themeResId) {
super(context, themeResId);
} public static class Builder { private View mLayout; private ImageView mIcon;
private TextView mTitle;
private TextView mMessage;
private Button mButton; private View.OnClickListener mButtonClickListener; private InfoDialog mDialog; public Builder(Context context) {
mDialog = new InfoDialog(context, R.style.Theme_AppCompat_Dialog);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
mLayout = inflater.inflate(R.layout.dialog, null, false);
//添加布局文件到 Dialog
mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)); mIcon = mLayout.findViewById(R.id.dialog_icon);
mTitle = mLayout.findViewById(R.id.dialog_title);
mMessage = mLayout.findViewById(R.id.dialog_message);
mButton = mLayout.findViewById(R.id.dialog_button);
} /**
* 通过 ID 设置 Dialog 图标
*/
public Builder setIcon(int resId) {
mIcon.setImageResource(resId);
return this;
} /**
* 用 Bitmap 作为 Dialog 图标
*/
public Builder setIcon(Bitmap bitmap) {
mIcon.setImageBitmap(bitmap);
return this;
} /**
* 设置 Dialog 标题
*/
public Builder setTitle(@NonNull String title) {
mTitle.setText(title);
mTitle.setVisibility(View.VISIBLE);
return this;
} /**
* 设置 Message
*/
public Builder setMessage(@NonNull String message) {
mMessage.setText(message);
return this;
} /**
* 设置按钮文字和监听
*/
public Builder setButton(@NonNull String text, View.OnClickListener listener) {
mButton.setText(text);
mButtonClickListener = listener;
return this;
} public InfoDialog create() {
mButton.setOnClickListener(view -> {
mDialog.dismiss();
mButtonClickListener.onClick(view);
});
mDialog.setContentView(mLayout);
mDialog.setCancelable(true); //用户可以点击后退键关闭 Dialog
mDialog.setCanceledOnTouchOutside(false); //用户不可以点击外部来关闭 Dialog
return mDialog;
}
}
}

弹出:

 public class MainActivity extends AppCompatActivity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.button_show_dialog).setOnClickListener(v -> {
InfoDialog infoDialog = new InfoDialog.Builder(this)
.setTitle("Done")
.setMessage("Something done")
.setButton("OK", view ->
Toast.makeText(this, "OK Clicked.", Toast.LENGTH_SHORT).show()
).create();
infoDialog.show();
});
}
}

Android 自定义AlertDialog的实现的更多相关文章

  1. Android 自定义AlertDialog退出对话框

    Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...

  2. Android自定义AlertDialog

    常见的一种方法: [html] view plaincopyprint? AlertDialog.Builder builder; AlertDialog alertDialog; LayoutInf ...

  3. android 自定义AlertDialog(一段)

    java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...

  4. Android 自定义AlertDialog(退出提示框)

    有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...

  5. Android 自定义AlertDialog的写法和弹出软键盘和覆盖状态栏

    private void showMyDialog(int layoutId){ AlertDialog myDialog = new AlertDialog.Builder(context).cre ...

  6. android 自定义alertdialog和取消dialog

    看代码: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle ...

  7. android 自定义AlertDialog

    xml: alter_dialog_two <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  8. Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)

    我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...

  9. Xamarin.Android 记事本(二)自定义AlertDialog

    导读 1.自定义一个AlertDialog 2.添加一条数据 正文 记事本应当有一个添加功能,这里我打算在右上角放一个item,然后点击这个item弹出一个对话框,输入名称,点击确定跳转到另一个act ...

随机推荐

  1. 原生JS实现弹幕效果

    纯属无聊写的,可能有很多问题,欢迎批评指教. 效果图:图一是预设的一些弹幕,图二是自己发射的弹幕,效果是一样的.   首先是弹幕的位置,是要从最右滑到最左,为了防止随机高度弹幕会覆盖的问题,设置了通道 ...

  2. PCA实现教程

    数据是机器学习模型的生命燃料.对于特定的问题,总有很多机器学习技术可供选择,但如果没有很多好的数据,问题将不能很好的解决.数据通常是大部分机器学习应用程序中性能提升背后的驱动因素. 有时,数据可能很复 ...

  3. 基本排序算法Golang

    摘要 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 冒泡排序 func BubbleSort(ve ...

  4. 关于kubernetes使用私有仓库一点说明

    一.概述 关于kubernetes使用私有docker image  registry的一些说明: 1.对于自己构建的项目镜像或一些不想暴露到外网的image需要使用自建的私有仓库,一般有两种选择:d ...

  5. Ubuntu apt-get和pip国内源更换

    Ubuntu apt-get和pip源更换 更新数据源为国内,是为了加速安装包的增加速度. 更换apt-get数据源 输入:sudo -s切换为root超级管理员: 执行命令:vim /etc/apt ...

  6. 五种IO模型透彻分析

    1.基础 在引入IO模型前,先对io等待时某一段数据的"经历"做一番解释.如图: 当某个程序或已存在的进程/线程(后文将不加区分的只认为是进程)需要某段数据时,它只能在用户空间中属 ...

  7. rsync+inotify实现全网自动化数据备份-技术流ken

    rsync简介 “rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步” rsync的功能和特点 ...

  8. c++选择重载函数

    一.函数重载 普通函数重载的关键是参数列表---也称函数特征标.函数参数中有以下情况可以出现重载: 1.  形参个数不同 2.  形参的类型不同 3.  形参的类型和个数都不同 const形参和函数重 ...

  9. Dapper批量更新

    本次示例项目使用Dappe1.50.5和Dapper.Contrib1.50.5 数据库执行的脚本检测工具是SQL Server Prifiler 1.使用Where In 实现批量更新 1.1代码如 ...

  10. Postgresql ODBC驱动,用sqlserver添加dblink跨库访问postgresql数据库

    在同样是SQLserver数据库跨库访问时,只需要以下方法 declare @rowcount int set @rowcount =(select COUNT(*) from sys.servers ...