一個(gè)開源項(xiàng)目實(shí)現(xiàn)沉浸式狀態(tài)欄 簡(jiǎn)單高效
GitHub地址: https://github.com/jgilfelt/SystemBarTint
這個(gè)star還是很多的
%V59S4[%(G{XOS6%S$OS]@N.png
使用步驟
- 添加依賴
//狀態(tài)欄
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
(1LRHY3NI[J4LT7]69{9DZR.png
- 在布局文件中添加屬性
android:fitsSystemWindows="true"
android:clipToPadding="false"
android:fitsSystemWindows="true"
作用就是你的contentview是否忽略actionbar,title,屏幕的底部虛擬按鍵,將整個(gè)屏幕當(dāng)作可用的空間佳头。
正常情況,contentview可用的空間是去除了actionbar,title,底部按鍵的空間后剩余的可用區(qū)域;這個(gè)屬性設(shè)置為true,則忽略梦谜,false則不忽略
android:clipToPadding="false"
clipToPadding:控件的繪制區(qū)域是否在padding里面, 值為true時(shí)padding那么繪制的區(qū)域就不包括padding區(qū)域;
- 核心代碼就是個(gè)工具類
private void initSystemBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
//修改window的綜合屬性flags
//WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS含義為狀態(tài)欄透明
winParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
win.setAttributes(winParams);
}
//調(diào)用開源庫(kù)SystemBarTintManager進(jìn)行狀態(tài)欄著色 產(chǎn)生沉浸式效果
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);//使用狀態(tài)欄著色可用
tintManager.setStatusBarTintColor(Color.GREEN);//指定顏色進(jìn)行著色
}
快捷使用
本人在項(xiàng)目中為了減小依賴的項(xiàng)目 只是把SystemBarTintManager這個(gè)管理類復(fù)制到項(xiàng)目中即可正常使用 無需再去進(jìn)行依賴