Android开发之沉浸状态栏

Android开发之沉浸状态栏

protected void setStatusBar() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上

        View decorView = getWindow().getDecorView();

int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;

decorView.setSystemUiVisibility(option);

//根据上面设置是否对状态栏单独设置颜色

        if (useThemestatusBarColor) {

getWindow().setStatusBarColor(getResources().getColor(R.color.white));//设置状态栏背景色

        }else {

getWindow().setStatusBarColor(Color.TRANSPARENT);//透明

        }

}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0

        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();

localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

}else {

Toast.makeText(this,"低于4.4的android系统版本不存在沉浸式状态栏", Toast.LENGTH_SHORT).show();

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&useStatusBarColor) {//android6.0以后可以对状态栏文字颜色和图标进行修改

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

}

}

你可能感兴趣的:(Android开发之沉浸状态栏)