zoukankan      html  css  js  c++  java
  • Android 自定义dialog类

    首先定制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()
  • 相关阅读:
    如何查看RabbitMQ日志,Rabbitmq Trace日志的使用
    windows激活 RabbitMQ's Management Plugin(必须)
    UNET
    边缘检测
    Huber Loss
    深度学习之自编码器AutoEncoder(一)
    PU learning简介
    机器学习-稀疏矩阵的处理
    R语言入门-安装R和Rstuido软件
    归一化 (Normalization)、标准化 (Standardization)和中心化/零均值化 (Zero-centered)
  • 原文地址:https://www.cnblogs.com/rchao/p/11598079.html
Copyright © 2011-2022 走看看