在 BottomSheetDialogFragment 中安排布局,以便所有视图都符合预期
Arranging layouts inside BottomSheetDialogFragment so that all Views fit as intended
我有一个 BottomSheetDialogFragment
,它有一些内容,底部有一个蓝色按钮 (TextView
)。我希望它始终固定在 BottomSheetDialogFragment
的底部,但我无法这样做。大多数时候它都在正确的位置,但有时它看起来有点低于,就像这个屏幕截图如下:
如何确保蓝色按钮始终固定在 BottomSheetDialogFragment
的底部,无论如何?根视图是 RelativeLayout
,当我尝试设置 android:layout_alignParentBottom="true"
但没有成功。
只是一个黑客
- 将底部按钮放入 activity/ 片段(底部 sheet 的父级)。
- 当底部 sheet 可见时使其可见。
- 将按钮高度与底部底部的边距相等sheet。 (不被按钮重叠)
- 关闭底部sheet 时使按钮不可见。
问题已解决。 :)
所以,我找到了 2 个可能的解决方案来解决这个问题,因此我在这里分享它们。
第一个解决方案是 BottomSheetDialogFragment
扩展到全屏,下面是如何完成的示例:
View parent = (View) view.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
然后,我们调整 BottomSheetDialogFragment
布局中按钮的位置,例如,如果我们有 RelativeLayout
,我们可以设置 android:layout_alignParentBottom="true"
。这将提供当 BottomSheetDialogFragment
展开(全屏)时,我们的按钮将始终与屏幕底部对齐。
另一种解决方案,我认为是 正确的解决方案 是将 BottomSheetDialogFragment
布局的所有内容包含在 ScrollView
中。这样,如果设备的高度不够,内容(包括按钮)将被定位,但可以随时滚动到。
我有一个 BottomSheetDialogFragment
,它有一些内容,底部有一个蓝色按钮 (TextView
)。我希望它始终固定在 BottomSheetDialogFragment
的底部,但我无法这样做。大多数时候它都在正确的位置,但有时它看起来有点低于,就像这个屏幕截图如下:
如何确保蓝色按钮始终固定在 BottomSheetDialogFragment
的底部,无论如何?根视图是 RelativeLayout
,当我尝试设置 android:layout_alignParentBottom="true"
但没有成功。
只是一个黑客
- 将底部按钮放入 activity/ 片段(底部 sheet 的父级)。
- 当底部 sheet 可见时使其可见。
- 将按钮高度与底部底部的边距相等sheet。 (不被按钮重叠)
- 关闭底部sheet 时使按钮不可见。
问题已解决。 :)
所以,我找到了 2 个可能的解决方案来解决这个问题,因此我在这里分享它们。
第一个解决方案是 BottomSheetDialogFragment
扩展到全屏,下面是如何完成的示例:
View parent = (View) view.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
然后,我们调整 BottomSheetDialogFragment
布局中按钮的位置,例如,如果我们有 RelativeLayout
,我们可以设置 android:layout_alignParentBottom="true"
。这将提供当 BottomSheetDialogFragment
展开(全屏)时,我们的按钮将始终与屏幕底部对齐。
另一种解决方案,我认为是 正确的解决方案 是将 BottomSheetDialogFragment
布局的所有内容包含在 ScrollView
中。这样,如果设备的高度不够,内容(包括按钮)将被定位,但可以随时滚动到。