Android 如何在BottomSheetDialog上使用视图绑定
我试图在MainActivity中创建一个BottomSheetDialog,但现在我正在使用synthetic来绑定我的视图。但现在我一直在研究如何将视图绑定附加到我的BottomSheetDialog 这就是我所做的,使用合成橡胶Android 如何在BottomSheetDialog上使用视图绑定,android,kotlin,android-viewbinding,android-bottomsheetdialog,Android,Kotlin,Android Viewbinding,Android Bottomsheetdialog,我试图在MainActivity中创建一个BottomSheetDialog,但现在我正在使用synthetic来绑定我的视图。但现在我一直在研究如何将视图绑定附加到我的BottomSheetDialog 这就是我所做的,使用合成橡胶 dialog = BottomSheetDialog(this, R.style.BottomDialogStyle) dialogView = LayoutInflater.from(this).inflate(R.layout.test_dialog, dia
dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
dialogView = LayoutInflater.from(this).inflate(R.layout.test_dialog, dialog.mainContainer, false)
dialog.setContentView(dialogView)
下面是让我困惑的是视图绑定
dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
binding = TestDialogBinding.inflate(?)
dialog.setContentView(binding.root)
从我上面的例子中,我想知道应该用什么填充参数,因为在活动中,我只能用
layoutInflater
填充该参数,而在RecyclerView适配器中,我不能用layoutInflater.From(parent.context)
,parent
填充该参数,和false
您还可以创建布局平面:
val inflater = LayoutInflater.from(requireContext())
然后将充气机传递至:
binding = TestDialogBinding.inflate(inflater)
如前所述,用于绑定:
bottomSheetDialog = BottomSheetDialog(mContext, R.style.BottomSheetDialogStyle)
mBottomSheetBinding = TestDialogBinding.inflate(layoutInflater, null, false)
bottomSheetDialog.setContentView(mBottomSheetBinding!!.root)
bottomSheetDialog.show()
我尝试使用
requireContext()
,但该函数在我的MainActivity中不可用。如果我只使用dialog.layoutInflater可以吗?如果你在活动中初始化ViewBinding,你只需要将这个(你的main活动)放入layoutInflater.from(this);