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

时间:2016-10-19 14:27:11

标签: java android bottom-sheet

我已在我的活动的onCreate()方法中实施了一个底页,如下所示:

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

我的活动扩展为AppCompatActivity,其中包含TabLayoutViewPager ..几乎是使用素材设计的标准用户界面。

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

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

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

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

黑色状态栏(为简单起见,删除了UI内容) Black Status Bar (UI content removed for simplicity)

3 个答案:

答案 0 :(得分: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);

对于设备&gt; API 20这个工作,并顺利过渡到我的statusBar更暗的阴影。希望能帮助到你。

答案 1 :(得分:1)

首先,一个类扩展了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);

答案 2 :(得分: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>
相关问题