首先定制style样式 styles.xml 加入自定义样式

<style name="CustomLoadingDialog">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">true</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@color/transparent</item><!--背景透明-->
<item name="android:background">@color/transparent</item>
<item name="android:backgroundDimEnabled">true</item><!--模糊-->
<item name="android:backgroundDimAmount">0.6</item>
</style>

在添加视图页面 在 res.layout 下加入loading_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="255"
android:background="@drawable/bg_white_radius"
android:gravity="center"
android:orientation="vertical"> <ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/content_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/loading_text"
android:textColor="@color/bg_primary"
android:textSize="14sp" /> </LinearLayout>

现在开始写自定义视图LoadingView

class LoadingView(context: Context, content: String) :
Dialog(context, R.style.CustomLoadingDialog) { init {
setContentView(R.layout.loading_dialog)
content_view.text = content
setCanceledOnTouchOutside(true)
backgroundAlpha(1f) val dm = context.resources.displayMetrics
window?.attributes?.width = (dm.widthPixels * 0.6).toInt()
window?.attributes?.height = (dm.widthPixels * 0.6 * 0.6).toInt()
setCancelable(false)
} override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_BACK -> {
if (isShowing) {
dismiss()
}
}
}
return super.onKeyDown(keyCode, event)
} override fun dismiss() {
if(isShowing){
super.dismiss()
}
} override fun setTitle(title: CharSequence?) {
content_view.text = title
} private fun backgroundAlpha(alpha: Float) {
window?.attributes?.alpha = alpha
} }

创建一个单例管理类,方便使用

class LoadingManager private constructor(){

    private var mContext:Context? = null
private var loadingView: LoadingView? = null companion object {
val instance: LoadingManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
LoadingManager()
}
} fun createDialog(context: Context, title: String): LoadingView {
if(mContext == context){
if(loadingView != null){
loadingView?.setTitle(title)
return loadingView!!
}
}
loadingView = null
mContext = context
loadingView = LoadingView(mContext!!, title)
return loadingView!!
} }

使用

//显示加载中视图
LoadingManager.instance.createDialog(this,"登录中").show()
//关闭视图
LoadingManager.instance.createDialog(this,"").dismiss()

Android 自定义dialog类的更多相关文章

  1. Android 自定义Dialog类,并在Activity中实现按钮监听。

      实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...

  2. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

  3. Android—自定义Dialog

    在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...

  4. Android自定义Dialog

    Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作, 如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作. Android系统提 ...

  5. Android自定义Dialog(美化界面)

    前言:在做项目的时候,发现dialog界面太丑陋,从csdn上下载了一份自定义dialog的源码,在他的基础上对界面进行美化...有需要的朋友可以直接拿走 效果图如下: 主要代码: /** * 自定义 ...

  6. Android自定义Dialog及其布局

     实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...

  7. android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...

  8. android 自定义Dialog去除黑色边框

    在自定义Dialog时显示的界面中老是有黑色的边框,下面就介绍使用style去除黑色边框方法. 首先在values/styles定义自定义样式: <style name="MyDial ...

  9. Android 自定义Dialog 去除阴影

    自定义Dialog中添加下列代码: window.clearFlags( WindowManager.LayoutParams.FLAG_DIM_BEHIND);

随机推荐

  1. 面试题(7)之 leetcode-003

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...

  2. JS事件-对象监视事件,事件触发函数

    写在前面: 一句话作为本文的核心思想:对象监视事件,事件触发函数: (一)事件 1.事件分类 (1)鼠标事件 click()                      鼠标单击 dblclick()  ...

  3. 每天一点点之vue框架开发 - axios解决跨越问题

    1.安装 npm install axios 或者 使用 bower: bower install axios 或者直接使用 cdn: <script src="https://unp ...

  4. git登录账号密码错误remote: Incorrect username or password (access token)

    git提交时弹框让输入用户和密码,不小心输入错误了 再次提交 一直就提示  remote: Incorrect username or password 错误了,也不弹框要重新输入 解决方法 win1 ...

  5. PyGame快速入门

    pygame 快速入门 目标 项目准备 使用 pygame 创建图形窗口 理解 图像 并实现图像绘制 理解 游戏循环 和 游戏时钟 理解 精灵 和 精灵组 项目准备 新建 飞机大战 项目 新建一个 h ...

  6. 动态改变tableHeaderView的显示隐藏及高度

    改变tableHeaderView的高度: UIView *headerView = _tableView.tableHeaderView; headerView.height = 10; 当设置高度 ...

  7. 吴裕雄--天生自然 JAVASCRIPT开发学习: 验证 API

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  8. 面试准备 DOM

    基本概念:Dom事件的级别 Dom0 级别 element.onclick=function() {} Dom1  没有制定事件相关的 Dom2 element.addEventListener(&q ...

  9. 销售de经典语录

    [销售的境界] 1.顾客要的不是便宜,而是感觉上占了便宜: 2.不要与顾客争论价格,要与顾客讨论价值: 3.没有不对的客户,只有不够好的服务: 4.卖什么不重要,重要的是怎么卖: 5.没有最好的产品, ...

  10. Pytorch学习--编程实战:猫和狗二分类

    Pytorch学习系列(一)至(四)均摘自<深度学习框架PyTorch入门与实践>陈云 目录: 1.程序的主要功能 2.文件组织架构 3. 关于`__init__.py` 4.数据处理 5 ...