Android 6.0以上沉浸式状态栏处理

styles文件修改

新建一个values-v23文件夹,建立styles.xml,然后建一个同名的theme

    

基类BaseActivity修改

1.获取状态栏高度

    public static int getStatusBarHeight(Context context) {
        Class c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

2.添加对于顶部栏的marginTop属性

        if (Build.VERSION.SDK_INT >= 23){
            View decorView = getWindow().getDecorView();
            View contentView = decorView.findViewById(android.R.id.content);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) contentView.getLayoutParams();
            layoutParams.topMargin = DisplayUtil.getStatusBarHeight(this);
            contentView.setLayoutParams(layoutParams);
        }

特殊场景兼容

代码动态修改状态栏颜色
设置状态栏字体黑色

   getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

设置状态栏字体白色

  getActivitySafely().getWindow().getDecorView().setSystemUiVisibility(0);

你可能感兴趣的:(Android 6.0以上沉浸式状态栏处理)