BottomSheetDialogFragment 没有出现

Sor*_*tfi 5 android kotlin bottom-sheet

我已经按照本教程在我的 android 应用程序中实现了 BottomSheetDiogFragment。

这是我的底部工作表布局(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>
Run Code Online (Sandbox Code Playgroud)

BottomSheetDialogFragment 类:

class BottomSheetTaskRepeat : BottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.bottom_sheet, container, false)
    }
}
Run Code Online (Sandbox Code Playgroud)

活动:

private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    bottomSheetTaskRepeat.show(supportFragmentManager, "my_bottom_sheet")
}
Run Code Online (Sandbox Code Playgroud)

问题是底页没有显示出来!任何帮助表示赞赏。

Xen*_*ion 6

这是一个迟来的答案,我正在为任何将面临同样问题的人写信,这就是我发现的:

由于某些原因,非约束视图高度在BottomSheetDialogFragment. 高度为 的视图wrap_content将不会显示。(但阴影会在那里),但是当你指定它的高度时,80dp它就会起作用。

对于这个问题,请更改您的RadioGroup身高并将其指定为:

android:layout_height="200dp"
Run Code Online (Sandbox Code Playgroud)

希望这有帮助。

BottomSheetDailogFragment更新:由于is的默认容器FrameLayout设置为WRAP_CONTENT,您可以在 Fragment 方法上重写它,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)

    }
}
Run Code Online (Sandbox Code Playgroud)