我创建了一个BottomSheetDialogFragment
,我想调整它的最大扩展高度。我怎么能这么做?我可以检索BottomSheetBehaviour
,但我所能找到的只是一个查看高度的设置器,但对于扩展的高度没有任何设置。
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
在顶部留出一个空间,它看起来很糟糕.
发布于 2017-09-26 22:44:50
高度被包装,因为充气视图被添加到具有FrameLayout的layout_height=wrap_content
中。见FrameLayout (R.id.design_bottom_sheet) at dialog.xml。
下面的类使底部全屏幕,背景透明,并充分扩展到顶部。
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日编辑--一年后我意识到背景被涂错了颜色。当用户拖动对话框时,这会将背景和内容一起拖动。我修复了它,以便底部工作表的父视图被着色。
发布于 2016-06-01 16:09:50
我找到了一个简单得多的答案;在您的示例中,您使用以下代码获得了底部工作表的FrameLayout
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
然后,可以将该视图的布局参数上的高度设置为要将展开的高度设置为任何高度。
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
发布于 2016-05-25 16:57:41
大更新,,避免重复代码,我给一个链接到全答,在那里你可以找到所有的解释,如何获得完整的行为,如Google。
我想调整它的最大扩展高度。我怎么能这么做?
BottomSheet
和BottomSheetDialogFragment
都使用您可以在Support 23.x中找到的BottomSheetBehavior
Java类对于mMinOffset
有两个不同的用途,其中一个用于定义它将用于绘制其内容的父类的区域(可能是一个NestedScrollView
)。另一个用途是定义扩展的锚点,我的意思是,如果你滑动它形成STATE_COLLAPSED
,它将动画你的BottomSheet
,直到他到达这个锚点,但如果你仍然可以继续滑动以覆盖所有父高度(CoordiantorLayout高度)。
如果您查看一下BottomSheetDialog
,您将看到以下方法:
private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
android.support.design.R.layout.design_bottom_sheet_dialog, null);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}
FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
if (params == null) {
bottomSheet.addView(view);
} else {
bottomSheet.addView(view, params);
}
// We treat the CoordinatorLayout as outside the dialog though it is technically inside
if (shouldWindowCloseOnTouchOutside()) {
final View finalView = view;
coordinator.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isShowing() &&
MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP &&
!coordinator.isPointInChildBounds(finalView,
(int) event.getX(), (int) event.getY())) {
cancel();
return true;
}
return false;
}
});
}
return coordinator;
}
不知道这两种行为中的哪一种,但如果需要第二种行为,请执行以下步骤:
CoordinatorLayout.Behavior<V>
扩展它BottomSheetBehavior
文件中的粘贴代码复制到新文件中。clampViewPositionVertical
:@覆盖公共int clampViewPositionVertical(查看子、int、int ){返回约束( top、mMinOffset、mHideable?mParentHeight : mMaxOffset);} int约束(整数,int低,int高){返回量<低?低:(数量>高?)高:数额);}
onLayoutChild
、onStopNestedScroll
、BottomSheetBehavior<V> from(V view)
和setState
(可选),下面是的样子
[
]
https://stackoverflow.com/questions/36030879
复制相似问题