赞
踩
private val binding: CoreDialogEditContentBinding by lazy {
CoreDialogEditContentBinding.inflate(mContext.layoutInflater)
}
private fun initWindowView() {
val view = LayoutInflater.from(mContext).inflate(R.layout.core_dialog_edit_content, null)
setContentView(view)
binding.editBtnConfirm.setOnClickListener(this)
binding.editBtnCancel.setOnClickListener(this)
}
通过binding设置的点击事件,那么setContent()中的参数使用应该如下,而不是重新手动生成一个View设置进去(建议:既然使用了dataBinding,那么调用控件视图的时候尽量用binding.view的方式)
private fun initWindowView() {
// val view = LayoutInflater.from(mContext).inflate(R.layout.core_dialog_edit_content, null)
// setContentView(view)
setContentView(binding.root)
binding.editBtnConfirm.setOnClickListener(this)
binding.editBtnCancel.setOnClickListener(this)
}
//c错误写法 private fun initWindowView() { binding.editBtnConfirm.setOnClickListener(this) binding.editBtnCancel.setOnClickListener(this) setCancelable(false) setCanceledOnTouchOutside(false) window?.attributes?.let { //显示位置 it.y = mContext.resources.getDimensionPixelSize(R.dimen.px380) //显示大小 it.width = mContext.resources.getDimensionPixelSize(R.dimen.px800) it.height = mContext.resources.getDimensionPixelSize(R.dimen.px660y) it.gravity = Gravity.TOP window?.setBackgroundDrawable(ColorDrawable()) window?.attributes = it } //设置视图 setContentView(binding.root) }
setContent()视图后,设置window才有效,分析: setAttributes(WindowManager.LayoutParams a)时,会通过dispatchWindowAttributesChanged() -> onWindowAttributesChanged()去更新dialog的根布局,将LayoutParams(大小位置等)设置进去,如果先设置window,然后设置setContent()视图,那么设置进去的视图实际上他的LayoutParams并不是你设置进去的,而是默认生成的,自然就没无效
//正确写法 private fun initWindowView() { //设置视图(放在设置window之前) setContentView(binding.root) binding.editBtnConfirm.setOnClickListener(this) binding.editBtnCancel.setOnClickListener(this) setCancelable(false) setCanceledOnTouchOutside(false) window?.attributes?.let { //显示位置 it.y = mContext.resources.getDimensionPixelSize(R.dimen.px380) //显示大小 it.width = mContext.resources.getDimensionPixelSize(R.dimen.px800) it.height = mContext.resources.getDimensionPixelSize(R.dimen.px660y) it.gravity = Gravity.TOP window?.setBackgroundDrawable(ColorDrawable()) window?.attributes = it } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。