首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BottomSheetDialogFragment没有出现

BottomSheetDialogFragment没有出现
EN

Stack Overflow用户
提问于 2019-01-01 16:35:57
回答 3查看 3.4K关注 0票数 7

我跟随本教程在我的android应用程序中实现了BottomSheetDiaogFragment。

这是我的底部工作表布局(bottom_sheet.xml):

代码语言:javascript
复制
<?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类:

代码语言:javascript
复制
class BottomSheetTaskRepeat : BottomSheetDialogFragment() {

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

活动:

代码语言:javascript
复制
private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()

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

    bottomSheetTaskRepeat.show(supportFragmentManager, "my_bottom_sheet")
}

问题是底纸没有出现!任何帮助都是非常感谢的。

EN

回答 3

Stack Overflow用户

发布于 2021-07-20 13:51:54

尝试以下几个方面:

  1. 在您的newInstance中创建和使用无参数静态方法BottomSheetDialogFragment,并在其上调用show()方法。
  2. 尝试LinearLayoutCompat作为根布局而不是ConstraintLayout
  3. 尝试一个彩色背景的根布局,以获得一个想法。
  4. 尝试根布局的match_parent高度。
  5. 确保没有立即调用dismiss()cancel()
  6. 检查能见度。
  7. 如果它包含一个recyclerView,请确保它有项,getItemCount没有返回0,并且我们正在正确地设置值!
  8. 如果您的更改由于Android错误而没有反映,请重新启动PC和设备。
票数 2
EN

Stack Overflow用户

发布于 2020-06-27 06:54:27

这是一个迟来的答复,我给那些将面临同样问题的人写信,这是我发现的:

由于某些原因,无约束视图高度在BottomSheetDialogFragment中不能工作。一个高度类似于wrap_content的视图不会显示。(但是阴影会在那里),但是当您指定它的高度类似于80dp时,它会工作。

对于此问题,请更改RadioGroup高度并将其指定为:

代码语言:javascript
复制
android:layout_height="200dp"

希望这是有帮助的。

UPDATE:由于BottomSheetDailogFragment的默认容器是FrameLayout,并且设置为WRAP_CONTENT,所以您可以在片段onStart方法上重写它,如下所示(Kotlin):

代码语言:javascript
复制
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)

    }
}
票数 1
EN

Stack Overflow用户

发布于 2022-05-28 07:09:06

如果要从内部片段中膨胀,则必须使用supportFragmentManager,而不是使用childFragmentManager,。

代码语言:javascript
复制
private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()
bottomSheetTaskRepeat.show(childFragmentManager, "my_bottom_sheet")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53997134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档