【问题标题】:Sometimes BottomSheetDialogFragment opens in center of screen like alert dialog有时,BottomSheetDialogFragment 在屏幕中心打开,如警报对话框
【发布时间】:2021-04-22 14:12:57
【问题描述】:

我有BottomSheetDialogFragment,它需要在展开状态下打开,并且应该在向下滑动时跳过折叠状态。

问题是,我有时会遇到意外的行为

为什么我有时会得到这种看法?

下面是我的代码,

@Override
    public int getTheme() {
        if (Build.VERSION.SDK_INT > 21) {
            return R.style.BottomSheetDialogTheme;
        } else {
            return super.getTheme();
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

        dialog.setOnShowListener((DialogInterface.OnShowListener) dialog1 -> {
            BottomSheetDialog d = (BottomSheetDialog) dialog1;
            FrameLayout bottomSheet = (FrameLayout) d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
            if (bottomSheet != null) {
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
                BottomSheetBehavior.from(bottomSheet).setFitToContents(true);
                BottomSheetBehavior.from(bottomSheet).setPeekHeight(1000);
            }
        });

        return dialog;
    }

同样的风格/主题是

     <style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog">
            <item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent
            </item>
        </style>
        <!-- set the rounded drawable as background to your bottom sheet -->
        <style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
            <item name="android:background">@drawable/bottomsheet_round_corner_bg</item>
        </style>
    
        <style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
            <item name="android:windowIsFloating">false</item>
            <item name="bottomSheetStyle">@style/BottomSheet</item>
            <item name="android:windowSoftInputMode">adjustResize</item>
        </style>

主要是我完成了这部分,但不知何故出现了其他屏幕截图行为。

任何解决或遇到此问题的人都可以评论或回答解决方案, 谢谢。

【问题讨论】:

    标签: android android-bottomsheetdialog bottomsheetdialogfragment


    【解决方案1】:

    您可能需要添加 window.setGravity(Gravity.BOTTOM)。

    【讨论】:

    • 它的bottomsheet,默认不应该是bottom吧?
    • BottomSheetBehavior.from(bottomSheet).setPeekHeight(1000);你用这个,我看到一个回答说根据源码设置高度来设置Gravity.BOTTOM,但是我没时间看源码,你可以看一下onCreatView的源码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多