为什么BottomSheetDialog将状态栏变为黑色?

问题描述 投票:2回答:3

我已经在我的活动的onCreate()方法中实现了一个底页如下:

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

我的活动扩展了AppCompatActivity并且里面有TabLayoutViewPager ..几乎是使用材料设计的标准UI。

我遇到的问题是,当调用show()时,状态栏会立即变为黑色,为底部页面扩展的平滑动画添加跳跃性,更不用说改变应用程序的配色方案了。

我希望状态栏保持与我的主题相关联的默认颜色,并在底部工作表展开时平滑变暗。

我做了很多搜索,但未能找到具体的解决方案。

可能导致此问题的任何想法以及我可以采取哪些步骤来修复它?

黑色状态栏(为简单起见,删除了UI内容)

java android bottom-sheet
3个回答
3
投票

在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,这工作并平稳过渡到我的状态栏的较暗的阴影。希望能帮助到你。


1
投票

First, wirte a class extends BottomSheetDialog and override 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;
        }

    }

Then, set owner activity

    bottomSheetDialog.setOwnerActivity(this);

0
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.