解决使用BottomSheetDialog底部虚拟导航栏NavigationBar的颜色

最新版本的BottomSheetDialog状态栏发黑问题已经没有了但是虚拟导航栏还是会发黑。
华为手机在使用BottomSheetDialog的时候底部虚拟导航栏总是黑色的看着和App整个UI不搭配
解决使用BottomSheetDialog底部虚拟导航栏NavigationBar的颜色_第1张图片
这里可以看到底部是黑色,要想解决需要单独为底部虚拟导航栏设置一下

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setWhiteNavigationBar(@NonNull Dialog dialog) {
     
        Window window = dialog.getWindow();
        if (window != null) {
     
            DisplayMetrics metrics = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            GradientDrawable dimDrawable = new GradientDrawable();

            GradientDrawable navigationBarDrawable = new GradientDrawable();
            navigationBarDrawable.setShape(GradientDrawable.RECTANGLE);
            navigationBarDrawable.setColor(Color.WHITE);//这里设置颜色

            Drawable[] layers = {
     dimDrawable, navigationBarDrawable};

            LayerDrawable windowBackground = new LayerDrawable(layers);
            windowBackground.setLayerInsetTop(1, metrics.heightPixels);

            window.setBackgroundDrawable(windowBackground);
        }
    }

当然整个设置方法我也是从别人那里学的,地址如下
https://stackoverflow.com/questions/47553936/prevent-bottomsheetdialogfragment-covering-navigation-bar
上面操后就Ok了,效果如下
解决使用BottomSheetDialog底部虚拟导航栏NavigationBar的颜色_第2张图片
完整代码如下

//标签选择页面
public class TagDataSelectFragment extends BottomSheetDialogFragment {
     


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
     
        BottomSheetDialog dialog = new BottomSheetDialog(getActivity());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
     
            setWhiteNavigationBar(dialog);
        }

        return dialog;
    }

    @Override
    public void onStart() {
     
        super.onStart();
        //获取dialog对象
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        Window window = dialog.getWindow();
        //设置Dialog背景透明
        window.findViewById(R.id.design_bottom_sheet).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     
        View view = inflater.inflate(R.layout.fragment_tag_select, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
     
        super.onViewCreated(view, savedInstanceState);
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setWhiteNavigationBar(@NonNull Dialog dialog) {
     
        Window window = dialog.getWindow();
        if (window != null) {
     
            DisplayMetrics metrics = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            GradientDrawable dimDrawable = new GradientDrawable();
            
            GradientDrawable navigationBarDrawable = new GradientDrawable();
            navigationBarDrawable.setShape(GradientDrawable.RECTANGLE);
            navigationBarDrawable.setColor(Color.WHITE);

            Drawable[] layers = {
     dimDrawable, navigationBarDrawable};

            LayerDrawable windowBackground = new LayerDrawable(layers);
            windowBackground.setLayerInsetTop(1, metrics.heightPixels);

            window.setBackgroundDrawable(windowBackground);
        }
    }

}

你可能感兴趣的:(开发问题)