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

4 java android bottom-sheet

我在onCreate()我的活动方法中实现了一个底部工作表,如下所示:

bottomSheet = new BottomSheetDialog(this);
bottomSheet.setContentView(getLayoutInflater().inflate(R.layout.bottom_sheet, null));
bottomSheet.show();
Run Code Online (Sandbox Code Playgroud)

我的 Activity 扩展AppCompatActivity并包含一个TabLayoutViewPager.. 几乎是使用材料设计的标准 UI。

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

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

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

任何想法可能导致这种情况以及我可以采取哪些步骤来解决它?

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

小智 5

在你的styles.xml 中添加以下内容:

 <style name="BottomDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,您可以将其添加到您的对话框中:

bottomSheet.setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialogTheme);
Run Code Online (Sandbox Code Playgroud)

对于设备> API 20,这有效并平滑过渡到我的状态栏的较暗阴影。希望能帮助到你。


小智 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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

然后,设置所有者活动

    bottomSheetDialog.setOwnerActivity(this);
Run Code Online (Sandbox Code Playgroud)