第1步:
在項目moudle下的gradle文件里添加以下依賴
compile 'com.githang:status-bar-compat:0.3'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
第2步:
實現(xiàn)一個改變狀態(tài)欄顏色的工具類
public class ChangeStatusBarColor {
? ? 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.colorAccent);
? ? }
? ? @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);
? ? }
}
第3步:
在需要改變狀態(tài)欄顏色的地方調用改變狀態(tài)欄顏色的方法
//改變狀態(tài)欄的顏色
ChangeStatusBarColor.initSystemBar(this);
第四步(這一步視情況可以忽略):接下來我們看看效果圖(前一個圖是沒有設置顏色的效果圖,后一個是設置顏色的效果圖):
貌似已經(jīng)達到效果了,但仔細觀察占拍,好像標題欄被提上去了枚粘,就是說APP界面全屏了尼荆,狀態(tài)了蓋在了APP上砚蓬,恩圣勒,這并非我們想要的效果解虱,那如何將界面從狀態(tài)欄下部開始呢攘须,只需要在Activity的布局文件最外層控件加上一個屬性:
android:fitsSystemWindows="true"就可以啦!看下效果:
第五步(視情況可以忽略這一步):如果系統(tǒng)默認的標題欄還在殴泰,可以嘗試在values/styles.xml下放入如下
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
?<item name="colorPrimary">@color/colorPrimary
? <item name="colorPrimaryDark">@color/colorPrimaryDark
? ?<item name="colorAccent">@color/colorAccent
<style name="FullscreenTheme" parent="AppTheme">
? ? <item name="android:actionBarStyle">@style/FullscreenActionBarStyle
? ? <item name="android:windowActionBarOverlay">true
? ? <item name="android:windowBackground">@null
? ? <item name="metaButtonBarStyle">?android:attr/buttonBarStyle
? ? <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
? ? <item name="android:background">@color/black_overlay
</style>
原文:https://blog.csdn.net/gl_x85/article/details/78248857
原文:https://www.jb51.net/article/92150.htm