android - 在状态栏上使 bottomSheetDialog 全屏显示

标签 android android-dialogfragment bottom-sheet

我最近使用了 android.support.design.widget.BottomSheetDialogFragment。我想做一些类似于谷歌联系人应用程序的事情,它的 BottomSheet 可以覆盖工具栏和状态栏。但是,当我使用 BottomSheetDialogFragment 来实现它时,结果是这样的: My Implementation

如您所见, Activity 的工具栏仍然可见。这是我的 BottomSheetDialogFragment 代码:

public class KeyDetailFragment extends BottomSheetDialogFragment {
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {

        }
    };

    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getActivity(), R.layout.sheet_key, null);
        dialog.setContentView(contentView);
        View parent = (View) contentView.getParent();
        parent.setFitsSystemWindows(true);
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
        contentView.measure(0, 0);
bottomSheetBehavior.setPeekHeight(contentView.getMeasuredHeight());

        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        if (params.getBehavior() instanceof BottomSheetBehavior) {
                 ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
        }
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
    }
}

我引用了来源,发现一个属性让我感兴趣:

private static int getThemeResId(Context context, int themeId) {
    if (themeId == 0) {
        // If the provided theme is 0, then retrieve the dialogTheme from our theme
        TypedValue outValue = new TypedValue();
        if (context.getTheme().resolveAttribute(
                R.attr.bottomSheetDialogTheme, outValue, true)) {
            themeId = outValue.resourceId;
        } else {
            // bottomSheetDialogTheme is not provided; we default to our light theme
            themeId = R.style.Theme_Design_Light_BottomSheetDialog;
        }
    }
    return themeId;
}

这里的属性bottomSheetDialogTheme可能会改变bottom sheet的样式,但我不知道如何改变它,我怀疑这是否有效。有人可以给我解决方案来实现它可以覆盖工具栏和状态栏吗?

最佳答案

试试这个。它对我有用。

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View inflatedView = View.inflate(getContext(), R.layout.fragment_coupon, null);
    dialog.setContentView(inflatedView);


    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) inflatedView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = params.getBehavior();

    if (behavior != null && behavior instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
    }

    View parent = (View) inflatedView.getParent();
    parent.setFitsSystemWindows(true);
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
    inflatedView.measure(0, 0);
    DisplayMetrics displaymetrics = new DisplayMetrics();        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenHeight = displaymetrics.heightPixels;
    bottomSheetBehavior.setPeekHeight(screenHeight);

    if (params.getBehavior() instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
    }

    params.height = screenHeight;
    parent.setLayoutParams(params);
}

关于android - 在状态栏上使 bottomSheetDialog 全屏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36424964/

相关文章:

android - 运行应用程序时出现错误 "$flutterSdkpath\packages\flutter_tools\gradle\app_plugin_loader.gradle"

android - ListView 在隐藏键盘之前不会更新

机器人 : open keyboard in dialogfragment

android - 滑动 BottomSheet 时为箭头按钮设置动画

android - 使用 CursorLoader 和 LoaderManager 从 Android 应用程序中检索图像

android - 仅在当前未运行时启动应用程序

android - 可跨文本在对话框 fragment 中不起作用

android - 如何在 BottomSheetDialog android 的顶部中心添加关闭按钮?

android - Google 的 BottomSheet 出现在屏幕顶部

java - Android - 未经系统许可杀死另一个应用程序