在我的项目中使用BottomSheetDialogFragment
总是会出现错误:
android.view.InflateException: Binary XML file line #40: Could not inflate Behavior subclass androidx.coordinatorlayout.widget.bottom sheet behavior
Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass androidx.coordinatorlayout.widget.bottom sheet behavior
在活动中使用片段,但始终会得到上面的错误
button = findViewById(R.id.button)
val modalBottomSheet = ModalBottomSheetFragment()
button.setOnClickListener {
modalBottomSheet.show(supportFragmentManager, "tag")
}
我的片段:
class ModalBottomSheetFragment : BottomSheetDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_modal_bottom_sheet, container, false)
}
}
和我的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Hello Click me!" />
</LinearLayout>
答案 0 :(得分:1)
使用以下代码更新代码
class ModalBottomSheetFragment: BottomSheetDialogFragment() {
@SuppressLint("RestrictedApi")
override fun setupDialog(dialog: Dialog, style: Int) {
super.setupDialog(dialog, style)
//Set the custom view
val view = LayoutInflater.from(context).inflate(R.layout.fragment_modal_bottom_sheet, null)
dialog.setContentView(view)
}
}