自定义父类BaseDialog重写DialogFragment----子类简便实现dialog

极简dialog父类实现
子类只需重写getLayoutRes()获取布局、initView()初始化即可

对应的方法看名字即可,居中、铺满、居右,宽高占父布局的比例、、、

.java

abstract class BaseDialog : Dialog {
    protected var mContext: Context? = null
    private var mLastButterKnifeClickTime: Long = 0

    constructor(context: Context) : super(context, R.style.PXDialog) {
        init(context)
    }

    constructor(context: Context, defStyle: Int) : super(context, defStyle) {
        init(context)
    }

    private fun init(context: Context) {
        mContext = context
        setContentView(getLayoutRes())
        val window = window
        if (window != null) {
            window.setGravity(Gravity.BOTTOM)
            val layoutParams = window.attributes
            layoutParams.width =
                if (isWithWrapContent()) ViewGroup.LayoutParams.WRAP_CONTENT else ViewGroup.LayoutParams.MATCH_PARENT
            val d = setWithPercent()
            if (d > 0) {
                layoutParams.width =
                    (getContext().resources.displayMetrics.widthPixels * d).toInt()
            }
            layoutParams.height =
                if (isHeightWrapContent()) ViewGroup.LayoutParams.WRAP_CONTENT else ViewGroup.LayoutParams.MATCH_PARENT
            val h = setHeightPercent()
            if (h > 0) {
                layoutParams.height =
                    (getContext().resources.displayMetrics.heightPixels * h).toInt()
            }
            if (isGravityRight()) {
                layoutParams.gravity = Gravity.RIGHT
            } else {
                layoutParams.gravity = if (isGravityCenter()) Gravity.CENTER else Gravity.BOTTOM
            }
            window.attributes = layoutParams
        }
    }

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        initView()
    }

    fun beFastClick(): Boolean {
        val currentClickTime = System.currentTimeMillis()
        val flag = currentClickTime - mLastButterKnifeClickTime < 400L
        mLastButterKnifeClickTime = currentClickTime
        return flag
    }

    abstract fun initView()

    abstract fun getLayoutRes():Int
    fun isGravityCenter(): Boolean {
        return false
    }

    fun isWithWrapContent(): Boolean {
        return false
    }

    fun isGravityRight(): Boolean {
        return false
    }

    fun setWithPercent(): Double {
        return -0.1
    }

    fun setHeightPercent(): Double {
        return -0.1
    }

    fun isHeightWrapContent(): Boolean {
        return true
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值