簡單記錄,設定一體化狀態列背景顏色的方法
阿新 • • 發佈:2019-01-08
/** * 設定一體化狀態列顏色 * @param activity * @param color 狀態列顏色 */ public static void setStatusBarTint(Activity activity, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(activity, true); } SystemBarTintManager tintManager = new SystemBarTintManager(activity); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintColor(color); } @TargetApi(19) private static void setTranslucentStatus(Activity activity, boolean on) { Window win = activity.getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); }
此方法適用於高版本透明狀態列後設置狀態列顏色 另,activity的layout中設定了了android:fitsSystemWindows="true"