Android 弹出自定义对话框

Android在任意Activity界面弹出一个自定义的对话框,效果如下图所示:

准备一张小图片,右上角的小X图标64*64,close_icon.png,随便找个小图片代替;

第一步:样式添加,注意:默认在values->thems下,如果版本较高,请至values->style.xml内定义,将以下代码添加在</resource>之前

    <style name="CustomDialog" parent="android:style/Theme.Dialog"><!--背景颜色及和透明程度--><item name="android:windowBackground">@android:color/transparent</item><!--是否去除标题 --><item name="android:windowNoTitle">true</item><!--是否去除边框--><item name="android:windowFrame">@null</item><!--是否浮现在activity之上--><item name="android:windowIsFloating">true</item><!--是否模糊--><item name="android:backgroundDimEnabled">true</item></style><!--自定义dialog背景弹框设置--><style name="mydialog" parent="android:style/Theme.Dialog"><!-- 背景透明,设置圆角对话框必须设置背景透明,否则四角会有背景色小块--><item name="android:windowBackground">@android:color/transparent</item><!-- 没有标题 --><item name="android:windowNoTitle">true</item><!-- 背景模糊 --><item name="android:backgroundDimEnabled">true</item></style>

第二步:专门为它创建两个类:DialogView + DialogManager  

//DialogView.java
package com.example....//my packageimport android.app.Dialog;
import android.content.Context;
import android.view.Window;
import androidx.annotation.NonNull;public class DialogView extends Dialog {public DialogView(@NonNull Context context, int layout, int style, int gravity) {super(context, style);setContentView(layout);Window mWindow = getWindow();}
}
//DialogManager.java
package com.example....//my packageimport android.content.Context;
import android.view.Gravity;public class DialogManager {private static volatile DialogManager mInstance = null;private DialogManager() { }public static DialogManager getInstance() {if (mInstance == null) {synchronized (DialogManager.class) {if (mInstance == null) {mInstance = new DialogManager();}}}return mInstance;}public DialogView initView(Context context, int layout) {return new DialogView(context,layout, R.style.CustomDialog, Gravity.CENTER);}public DialogView initView(Context context,int layout,int gravity) {return new DialogView(context,layout, R.style.mydialog, gravity);} public void show(DialogView view) {//Showif (view != null) {if (!view.isShowing()) {view.show();}}}public void hide(DialogView view) {//Hideif (view != null) {if (view.isShowing()) {view.dismiss();}}}
}

第三步:给它创建样式布局xml   my_dlg_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:background="@color/white"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:orientation="vertical"android:layout_marginTop="5dp"android:layout_height="wrap_content"><RelativeLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_height="34dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="25dp"android:text="MyDialog"android:textColor="@color/red"android:textSize="16sp"android:textStyle="bold" /></RelativeLayout><RelativeLayoutandroid:layout_alignParentRight="true"android:layout_width="wrap_content"android:orientation="horizontal"android:layout_gravity="right"android:layout_marginRight="10dp"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/btn_cancel"android:layout_width="25dp"android:src="@drawable/close_icon"android:layout_margin="5dp"android:layout_height="25dp"/></RelativeLayout></RelativeLayout><Viewandroid:layout_width="match_parent"android:background="@color/gray"android:layout_height="1dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="名称:华山一区..."android:textSize="12sp"></EditText><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:textSize="12sp"android:hint="备注..."></EditText><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/MY_Test_Add"android:background="@color/red"android:textColor="@color/white"android:layout_margin="10dp"android:paddingTop="10dp"android:paddingBottom="10dp"android:text="添加"></Button></LinearLayout></LinearLayout></LinearLayout>

//这里用到了刚才提到的close_icon,随便替换为你的一个小图标

第四步:优化-圆角(可有可无)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="10dp"/><solid android:color="#FFEEEE" />
</shape>

//注意文件路径res/drawable/shapes.xml,添加进去别和你的东西冲突了,注意着点,边框颜色随便调整

第五步:已经完成了,分两步显示它:初始化+显示

import android.view.Gravity;//needed//myActivity(){.....private DialogView mDlgView;//公共变量
private ImageView btnCancel;//公共变量//protected void onCreate(Bundle savedInstanceState) {//my onCreate
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);mDlgView= DialogManager.getInstance().initView(this, R.layout.my_dlg_layout, Gravity.BOTTOM);//这里要注意,这个对话框的View要单独绑定自己的布局
mDlgView.setCanceledOnTouchOutside(false);//这是设置区域外点击是否取消显示
btnCancel = mDlgView.findViewById(R.id.btn_cancel);//注意这个关闭图片X,在对话框布局里了,而不是在当前页面布局,不可用this.findViewBy...btnCancel.setOnClickListener(new OnClickListener() {//给返回按纽添加点击隐藏事件@Overridepublic void onClick(View view) {DialogManager.getInstance().hide(mDlgView);}
});

初始化完毕,在需要的地方进行调用,比如你的按钮被点击了,直接在里调用这一句即可;

DialogManager.getInstance().show(mDlgView);

更多操作提示:

//mDlgView.dismiss(); //取消
//mDlgView.setCanceledOnTouchOutside(true);//允许区域外点击关闭
//mDlgView.setCanceledOnTouchOutside(false);//禁止区域外点击关闭//每次显示的时候其实应该清空Edittext里面的内容,返回关闭X的图标的ID都能绑定了,相同的方法上面的任何子控件绑定都是小菜一碟,给个ID,用mDialogView.findViewById(R.....)就出来了//my_dlg_layout.xml 样式随便调 padding是内部边距,margin是外边距
//那一根线条的颜色也是可调的,高度为1的View,android:background="@color/gray",你甚至可以改为:android:background="#AAAAAA"举一反三,祝你成功!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/192920.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于SSM的在线投稿系统设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 目录…

RAAGR2-Net:一种使用多个空间帧的并行处理的脑肿瘤分割网络

RAAGR2-Net: A brain tumor segmentation network using parallel processing of multiple spatial frames RAAGR2-Net&#xff1a;一种使用多个空间帧的并行处理的脑肿瘤分割网络背景贡献实验N4 bias-field-correction 数据预处理Z-score and re-sampling Z-score归一化&#…

快速入门:构建您的第一个 .NET Aspire 应用程序

##前言 云原生应用程序通常需要连接到各种服务&#xff0c;例如数据库、存储和缓存解决方案、消息传递提供商或其他 Web 服务。.NET Aspire 旨在简化这些类型服务之间的连接和配置。在本快速入门中&#xff0c;您将了解如何创建 .NET Aspire Starter 应用程序模板解决方案。 …

Ubuntu 22.04安装Rust编译环境并且测试

我参考的博客是《Rust使用国内Crates 源、 rustup源 |字节跳动新的 Rust 镜像源以及安装rust》 lsb_release -r看到操作系统版本是22.04,uname -r看到内核版本是uname -r。 sudo apt install -y gcc先安装gcc&#xff0c;要是结果给我的一样的话&#xff0c;那么就是安装好了…

NPM 与 XUI 共存!Nginx Proxy Manager 搭配 X-UI 实现 Vless+WS+TLS 教程!

之前分享过搭建可以与宝塔共存的一个 “魔法” 服务器状态监控应用 ——xui&#xff0c;支持 VmessWSTLS。 最近 Docker 视频出的比较多&#xff0c;前阵子又出现了宝塔国内版存在隐私泄露的问题&#xff0c;很多小伙伴其实都不用宝塔了&#xff0c;那么&#xff0c;在我们现在…

基于django的在线教育系统

基于python的在线教育系统 摘要 基于Django的在线教育系统是一种利用Django框架开发的现代化教育平台。该系统旨在提供高效、灵活、易用的在线学习体验&#xff0c;满足学生、教师和管理员的需求。系统包括学生管理、课程管理、教师管理、视频课程、在线测验等核心功能。系统采…

python趣味编程-5分钟实现一个打字速度测试(含源码、步骤讲解)

Python速度打字测试是用 Python 编程语言编写的,速度打字测试 Python项目理念,我们将构建一个令人兴奋的项目,通过它您可以 检查 甚至 提高 您的打字速度。 为了创建图形用户界面(GUI),我们将使用 用于处理图形的pygame库。 Python 打字速度测试有利于学生或初学者提高…

python中的NumPy和Pandas往往都是同时使用,NumPy和Pandas的在数据分析中的联合使用

文章目录 前言一、numpy的介绍与用法二、pandas的介绍与用法三、numpy与pandas的联合使用说明四、numpy与pandas的联合使用程序代码4.1 读取CSV文件并进行数据清洗&#xff0c;如去除NaN值4.2 矩阵操作和特征工程&#xff0c;如标准化处理4.3 使用Pandas进行数据筛选和分组聚合…

Android resource/drawable转换成Uri,Kotlin

Android resource/drawable转换成Uri&#xff0c;Kotlin private fun convertResource2Uri(resId: Int): Uri {return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE "://" resources.getResourcePackageName(resId) / resources.getResourceTypeName(resI…

golang学习笔记——接口

文章目录 Go 语言接口例子空接口空接口的定义空接口的应用空接口作为函数的参数空接口作为map的值 类型断言接口值 类型断言例子001类型断言例子002 Go 语言接口 接口&#xff08;interface&#xff09;定义了一个对象的行为规范&#xff0c;只定义规范不实现&#xff0c;由具…

基于传统Session的登录

前言&#xff1a; 本人的一些简历上要回答的点。所以再此整理。 亮点&#xff1a; 使用Filter过滤器进行未登录状态自动跳转到登录页面的拦截&#xff0c;实现统一的权限管理。 1 登陆功能 1.1实体类和结果类 前端页面 约定 res.data.code为1时是登录成功。 数据库的empl…

【C++】【Opencv】cv::warpAffine()仿射变换函数详解,实现平移、缩放和旋转等功能

仿射变换是一种二维变换&#xff0c;它可以将一个二维图形映射到另一个二维图形上&#xff0c;保持了图形的“形状”和“大小”不变&#xff0c;但可能会改变图形的方向和位置。仿射变换可以用一个线性变换矩阵来表示&#xff0c;该矩阵包含了六个参数&#xff0c;可以进行平移…