我在我的 BottomSheetDialogFragment
中遇到了一些显示按钮的问题。我希望它能够固定在底部工作表上,无论该工作表是展开还是折叠。
请参见下面的图片:
(我使用 Sketch 创建了这个图片)
有什么提示或技巧吗?
顺便说一下,如果您知道如何向 bottomsheetdialog
添加顶部边距,那就更好了;)
我在我的 BottomSheetDialogFragment
中遇到了一些显示按钮的问题。我希望它能够固定在底部工作表上,无论该工作表是展开还是折叠。
请参见下面的图片:
(我使用 Sketch 创建了这个图片)
有什么提示或技巧吗?
顺便说一下,如果您知道如何向 bottomsheetdialog
添加顶部边距,那就更好了;)
感谢 @Gnocalo 发布的帖子。我已经实现了它,请在我的 Github 上找到。
步骤:
BottomSheetDialogFragment
val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bottomSheetDialog.setOnShowListener {
val dialog = it as BottomSheetDialog
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout
val containerLayout: FrameLayout =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.container) as FrameLayout
val button = binding.submitButton
val parent = button.parent as ViewGroup
parent.removeView(button)
containerLayout.addView(button, containerLayout.childCount)
}
return bottomSheetDialog
我正在寻找一种解决粘性底部按钮的方案,但也想回答您关于设置底部工作表边距的附加问题。
因此,您无法向您的底部工作表添加边距,它基本上是由底部工作表状态处理的。
您可以通过以下方式明确设置底部工作表的状态:
val bottomSheet = dialog!!.findViewById<View>(design_bottom_sheet) as FrameLayout
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from<View>(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
可能需要一些时间,但我遇到了同样的问题,所以决定写一篇文章:在底部表单对话框中添加 Android 粘性视图
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@id/button"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>