一个使用ViewBinding封装的Dialog

一个使用ViewBinding封装的Dialog,使用方便简单

话不多说,直接上干货

使用方法

kotlin 复制代码
showXDialog(DialogCustomBinding::inflate) {
    setCancelable(true)
    setCanceledOnTouchOutside(true)
    setDimAmount(0.7f)
    setWidth(300)
    setGravity(Gravity.CENTER)
    binding.apply {
        dialogButtonConfirm.setOnClickListener {
            toast("确定")
            dismiss()
        }
        dialogButtonCancel.setOnClickListener {
            toast("取消")
            dismiss()
        }
    }

}

实现方法

1、先封装一个 DialogScope
kotlin 复制代码
class DialogScope<VB : ViewBinding>(
    val dialog: Dialog,
    val binding: VB
) {

    private val window get() = dialog.window
    private val context get() = dialog.context

    private val params: WindowManager.LayoutParams?
        get() = window?.attributes

    /* ---------------- 基础 ---------------- */

    fun setCancelable(flag: Boolean) {
        dialog.setCancelable(flag)
    }

    fun setCanceledOnTouchOutside(flag: Boolean) {
        dialog.setCanceledOnTouchOutside(flag)
    }

    fun setDimAmount(amount: Float) {
        window?.setDimAmount(amount)
    }

    fun dismiss() {
        dialog.dismiss()
    }

    /* ---------------- 固定宽高 ---------------- */

    fun setWidth(widthDp: Int) {
        params?.let {
            it.width = widthDp.dp
            window?.attributes = it
        }
    }

    fun setHeight(heightDp: Int) {
        params?.let {
            it.height = heightDp.dp
            window?.attributes = it
        }
    }

    /* ---------------- 屏幕比例 ---------------- */

    fun setScreenWidthAspect(aspect: Float) {
        val screenWidth = context.resources.displayMetrics.widthPixels
        params?.let {
            it.width = (screenWidth * aspect).toInt()
            window?.attributes = it
        }
    }

    fun setScreenHeightAspect(aspect: Float) {
        val screenHeight = context.resources.displayMetrics.heightPixels
        params?.let {
            it.height = (screenHeight * aspect).toInt()
            window?.attributes = it
        }
    }

    /* ---------------- 重力 ---------------- */

    fun setGravity(gravity: Int) {
        params?.let {
            it.gravity = gravity
            window?.attributes = it
        }
    }
}

2、再封装Dialog

kotlin 复制代码
class XDialog<VB : ViewBinding>(
    private val inflater: (LayoutInflater) -> VB
) : DialogFragment() {

    private var _binding: VB? = null
    private val binding get() = _binding!!

    private var dslBlock: (DialogScope<VB>.() -> Unit)? = null

    fun setDsl(block: DialogScope<VB>.() -> Unit) {
        dslBlock = block
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return Dialog(requireContext()).apply {
            requestWindowFeature(Window.FEATURE_NO_TITLE) // 防止 requestFeature() 异常
        }
    }

    override fun onResume() {
        //去除左右边距
        dialog!!.window!!.decorView.setPadding(0, 0, 0, 0)
        super.onResume()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = this.inflater.invoke(inflater)
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.let { window ->
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            // 先给一个默认值,防止 wrap_content 失效
            val params = window.attributes
            params.width = ViewGroup.LayoutParams.WRAP_CONTENT
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT
            window.attributes = params
        }
        dialog?.let {
            dslBlock?.invoke(DialogScope(it, binding))
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
如果有帮助到你,请点赞收藏
最后附上源码地址

Gitee地址

相关推荐
এ慕ོ冬℘゜1 小时前
如何在 HTML 中嵌入音频和视频?
前端·html·音视频
变与不变80610 小时前
jQuery怎么使用
前端·javascript
神膘护体小月半10 小时前
flex 布局的 order 属性
前端·css
paopaokaka_luck10 小时前
流浪动物救助与收养系统(DeepSeek AI对话、 ECharts多维分析、救助流程衔接、领养与回访衔接、 捐赠确认与公示、志愿者协作)
java·前端·javascript·人工智能·mysql·echarts·推荐算法
仰望.10 小时前
vue 表格组件 vxe-table 树表格懒加载支持拖拽排序的详细用法
前端·javascript·vue.js·vxe-table
前端 贾公子10 小时前
vite-plugin-uni-manifest 升级指南
前端
小码哥_常10 小时前
iPhone 用户传了张 HEIC,我的前端崩了
前端
小码哥_常11 小时前
你发的原图可能暴露你家地址——聊聊图片 EXIF 隐私
前端
java1234_小锋11 小时前
【技术专题】CSS3 网页技术 - CSS3 优先级
前端·css·css3
鸿蒙开发12 小时前
共享 Schema 与 SchemaFactory
前端·后端·harmonyos