公眾號:smart_android
作者:耿廣龍|loonggg
點擊“閱讀原文”,可查看更多內容和干貨
效果圖
首先要在布局文件中加入下面兩個屬性:
android:clipToPadding="true"
android:fitsSystemWindows="true"
解釋一下上面兩個布局屬性的意思:
android:clipToPadding 定義布局間是否有間距
android:fitsSystemWindows="true" 意思就是設置應用布局時是否考慮系統窗口布局;如果為true,將調整系統窗口布局以適應你自定義的布局。比如系統有狀態欄,應用也有狀態欄時。看你這個布局代碼,恰恰是在定義標題欄樣式,所以用到這行代碼了。
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中即可。
想要源碼的,關注公眾號,發送關鍵字“源碼”即可獲得。
【特別推薦↓】
微信公眾號:smart_android ,公眾號[非著名程序員]可能是東半球最好的技術分享公眾號。每天,每周定時推送一些有關移動開發的原創文章和教程。
「非著名程序員」本人建立了一個高端Android微信交流群,如果有想加入的請先加我個人微信號:loonggg ,具體加入條件非常簡單,加我個人微信號時,請備注為:加群,到時會告訴你具體的加入流程,感謝