为什么BottomSheetDialog会使状态栏变黑?

4

我在我的活动的onCreate()方法中实现了底部菜单表:

bottomSheet = new BottomSheetDialog(this);
bottomSheet.setContentView(getLayoutInflater().inflate(R.layout.bottom_sheet, null));
bottomSheet.show();

我的活动扩展了 AppCompatActiviy,并且其中有一个 TabLayout 和 ViewPager,基本上使用了标准的 Material Design UI。现在遇到的问题是,在调用 show() 方法时,状态栏立即变黑,使得底部表格扩大的平滑动画出现抖动,更不用说改变应用程序的配色方案。
我希望状态栏保持与我的主题相关联的默认颜色,并在底部表格扩大时平滑变暗。
我已经搜索了很多,但没有找到明确的解决方案。您有什么想法造成这种问题,如果有的话,我可以采取哪些步骤来解决它?
黑色状态栏(为简洁起见删除了 UI 内容) Black Status Bar (UI content removed for simplicity)
4个回答

6
请在styles.xml中添加以下内容:
 <style name="BottomDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowTranslucentStatus">true</item>
</style>

您可以使用以下方式将其添加到对话框中:
bottomSheet.setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialogTheme);

对于API大于20的设备,这个方法可以使我的状态栏平滑地过渡到一个较暗的色调。希望这能帮助你。


2
<style name="BottomSheetStyle" parent="BottomSheetDialogTheme">
    <item name="android:backgroundDimEnabled">false</item>
</style>

使用backgroundDimEnabled = false并将此样式设置为您的BottomSheetDialog,以禁用状态栏调暗,如果您需要全屏底部表格,并且不需要背景调暗。

2

首先,编写一个继承BottomSheetDialog的类,并重写onCreate()方法:

public class FixedBottomSheetDialog extends BottomSheetDialog {

        public FixedBottomSheetDialog(@NonNull Context context) {
            super(context);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            int screenHeight = getScreenHeight(getOwnerActivity());
            int statusBarHeight = getStatusBarHeight(getContext());
            int dialogHeight = screenHeight - statusBarHeight;
            if (getWindow() != null)
                getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
        }

        private static int getScreenHeight(Activity activity) {
            DisplayMetrics displaymetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            return displaymetrics.heightPixels;
        }

        private static int getStatusBarHeight(Context context) {
            int statusBarHeight = 0;
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = res.getDimensionPixelSize(resourceId);
            }
            return statusBarHeight;
        }

    }

然后,设置所有者活动

    bottomSheetDialog.setOwnerActivity(this);

1
在`styles.xml`中添加:

<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">
    <item name="bottomSheetDialogTheme">@style/BaseBottomSheetDialog</item>
</style>

并且

<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_dialog</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:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@color/colorPrimary</item>
</style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接