我跟随本教程在我的android应用程序中实现了BottomSheetDiaogFragment。
这是我的底部工作表布局(bottom_sheet.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:text="@string/rb1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:text="@string/rb2" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>BottomSheetDialogFragment类:
class BottomSheetTaskRepeat : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bottom_sheet, container, false)
}
}活动:
private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bottomSheetTaskRepeat.show(supportFragmentManager, "my_bottom_sheet")
}问题是底纸没有出现!任何帮助都是非常感谢的。
发布于 2021-07-20 13:51:54
尝试以下几个方面:
newInstance中创建和使用无参数静态方法BottomSheetDialogFragment,并在其上调用show()方法。LinearLayoutCompat作为根布局而不是ConstraintLayout。match_parent高度。dismiss()或cancel()。recyclerView,请确保它有项,getItemCount没有返回0,并且我们正在正确地设置值!发布于 2020-06-27 06:54:27
这是一个迟来的答复,我给那些将面临同样问题的人写信,这是我发现的:
由于某些原因,无约束视图高度在BottomSheetDialogFragment中不能工作。一个高度类似于wrap_content的视图不会显示。(但是阴影会在那里),但是当您指定它的高度类似于80dp时,它会工作。
对于此问题,请更改RadioGroup高度并将其指定为:
android:layout_height="200dp"希望这是有帮助的。
UPDATE:由于BottomSheetDailogFragment的默认容器是FrameLayout,并且设置为WRAP_CONTENT,所以您可以在片段onStart方法上重写它,如下所示(Kotlin):
override fun onStart() {
super.onStart()
val containerID = com.google.android.material.R.id.design_bottom_sheet
val bottomSheet: FrameLayout? = dialog?.findViewById(containerID)
bottomSheet?.let {
BottomSheetBehavior.from<FrameLayout?>(it).state =
BottomSheetBehavior.STATE_HALF_EXPANDED
bottomSheet.layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT
}
view?.post {
val params = (view?.parent as View).layoutParams as (CoordinatorLayout.LayoutParams)
val behavior = params.behavior
val bottomSheetBehavior = behavior as (BottomSheetBehavior)
bottomSheetBehavior.peekHeight = view?.measuredHeight ?: 0
(bottomSheet?.parent as? View)?.setBackgroundColor(Color.TRANSPARENT)
}
}发布于 2022-05-28 07:09:06
如果要从内部片段中膨胀,则必须使用supportFragmentManager,而不是使用childFragmentManager,。
private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()
bottomSheetTaskRepeat.show(childFragmentManager, "my_bottom_sheet")https://stackoverflow.com/questions/53997134
复制相似问题