BottomSheetDialogFragment-如何设置扩展高度(或最小顶部偏移) [英] BottomSheetDialogFragment - How to set expanded height (or min top offset)
问题描述
我创建一个BottomSheetDialogFragment
,我想调整它的最大展开高度.我怎样才能做到这一点?我可以找到BottomSheetBehaviour
,但是我只能找到一个用于窥视高度的设置器,而不能用于扩展高度.
I create a BottomSheetDialogFragment
and I want to adjust it's maximum expanded height. How can I do that? I can retrieve the BottomSheetBehaviour
but all I can find is a setter for the peek height but nothing for the expanded height.
public class DialogMediaDetails extends BottomSheetDialogFragment
{
@Override
public void setupDialog(Dialog dialog, int style)
{
super.setupDialog(dialog, style);
View view = View.inflate(getContext(), R.layout.dialog_media_details, null);
dialog.setContentView(view);
...
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setPeekHeight(...);
// how to set maximum expanded height???? Or a minimum top offset?
}
}
编辑
我为什么需要那个?因为我在全屏活动中显示了BottomSheet
对话框,如果BottomSheet
在顶部留出一个空格,这看起来很糟糕...
Why do I need that? Because I show a BottomSheet
Dialog in a full screen activity and it looks bad if the BottomSheet
leaves a space on top...
推荐答案
由于高度膨胀的视图被添加到具有layout_height=wrap_content
的FrameLayout中,因此高度被包裹.请参阅 https中的FrameLayout(R.id.design_bottom_sheet). ://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_sheet_dialog.xml .
The height is being wrapped because of the inflated view is added to the FrameLayout which has layout_height=wrap_content
. See FrameLayout (R.id.design_bottom_sheet) at https://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_sheet_dialog.xml.
下面的类使底部工作表全屏显示,背景透明,并完全扩展到顶部.
The class below makes the bottom sheet full screen, background transparent and fully expanded to the top.
public class FullScreenBottomSheetDialogFragment extends BottomSheetDialogFragment {
@CallSuper
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
}
View view = getView();
view.post(() -> {
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());
((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT)
});
}
}
---编辑2018年8月30日--- 一年后,我意识到背景色错了.当用户拖动对话框时,这将背景和内容一起拖动. 我对其进行了修复,以使底部工作表的父视图带有颜色.
--- EDIT Aug 30, 2018 --- I realized a year later that the background was coloured on a wrong view. This dragged the background along with the content while a user was dragging the dialog. I fixed it so that the parent view of the bottom sheet is coloured.
这篇关于BottomSheetDialogFragment-如何设置扩展高度(或最小顶部偏移)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!