【问题标题】:BottomSheetDialogFragment is not showing upBottomSheetDialogFragment 未显示
【发布时间】:2019-01-01 16:35:57
【问题描述】:

我已经按照this tutorial 在我的 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")
}

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

【问题讨论】:

  • 你能设置一个断点来检查执行是否到达bottomSheetTaskRepeat.show..吗?因为我运行了你的代码并且它运行良好。
  • 检查底部对话框片段内是否有任何dismiss() 调用是个好主意。还要检查它被调用的地方。有时突然解雇也是这样。

标签: android kotlin bottom-sheet


【解决方案1】:

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

由于某些原因,不受约束的视图高度在 BottomSheetDialogFragment 中不起作用。高度为 wrap_content 的视图将不会显示。(但阴影会在那里),但是当您像 80dp 一样指定其高度时,它会起作用。

对于这个问题,去改变你的RadioGroup 高度并将它指定为:

android:layout_height="200dp"

希望对您有所帮助。

更新: 由于BottomSheetDailogFragment 的默认容器是FrameLayout 并设置为WRAP_CONTENT,因此您可以像这样(Kotlin)在您的片段onStart 方法上覆盖它:

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)

    }
}

【讨论】:

    【解决方案2】:

    尝试以下操作:

    1. 在您的BottomSheetDialogFragment 中创建并使用无参数静态方法newInstance 并在其上调用show() 方法。
    2. 尝试将LinearLayoutCompat 作为根布局,而不是ConstraintLayout
    3. 尝试在根布局中使用彩色背景以获得灵感。
    4. 尝试match_parent 高度作为根布局。
    5. 确保没有立即调用 dismiss()cancel()
    6. 检查可见性。
    7. 如果您的更改因 Android Studio 错误而未反映,请重新启动 PC 和设备。

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 2019-10-22
      • 2023-02-13
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2016-06-19
      • 2020-10-17
      相关资源
      最近更新 更多