Android之改變狀態列的顏色使其與APP風格一體化
效果圖
方法實現 1添加布局屬性2在Activity中應用一下方法首先要在佈局檔案中加入下面兩個屬性:
android:clipToPadding="true"
android:fitsSystemWindows="true"
解釋一下上面兩個佈局屬性的意思:
android:clipToPadding 定義佈局間是否有間距
android:fitsSystemWindows="true" 意思就是設定應用佈局時是否考慮系統窗口布局;如果為true,將調整系統窗口布局以適應你自定義的佈局。比如系統有狀態列,應用也有狀態列時。看你這個佈局程式碼,恰恰是在定義標題欄樣式,所以用到這行程式碼了。
3SystemBarTintManager 的使用public static void initSystemBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(activity, true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
// 使用顏色資源
tintManager.setStatusBarTintResource(R.color.status_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);
}
到這裡就介紹完了,是不是很簡單,趕緊去試試吧。SystemBarTintManager 是狀態列的管理例項,沒有它是不行的,它的開源地址是:https://github.com/hexiaochun/SystemBarTint ,已經封裝的非常好了,我們只需要把它下載下來,應用到你的App中即可。