创建布局文件 dialog_collect.xml
创建弹窗的样式文件 dialog_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" /> 圆角
<solid android:color="@color/white"/> 填充色
</shape>
创建弹窗类
class CollectDialog() : Dialog(context) {
init {
//DialogCollectBinding ViewBing自动创建的布局文件类,不知道的去学viewBinding
val binding = DialogCollectBinding.inflate(layoutInflater)
setContentView(binding.root)
window?.setBackgroundDrawableResource(R.drawable.dialog_bg)
// 设置Dialog的宽度和高度为match_parent
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
// 设置背景透明
//window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
//这里写具体的页面逻辑 点击事件之类的
}
}
使用
CollectDialog().show()