极简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
}
}