状态栏管理

第三方库ImmersionBar

第三方库StatusBarUtil

特性

  1. 设置状态栏颜色

    1
    StatusBarUtil.setColor(Activity activity, int color)
  2. 设置状态栏半透明

    1
    StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha)
  3. 设置状态栏全透明

    1
    StatusBarUtil.setTransparent(Activity activity)
  4. 为包含 DrawerLayout 的界面设置状态栏颜色(也可以设置半透明和全透明)

    1
    StatusBarUtil.setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color)
  5. 为使用 ImageView 作为头部的界面设置状态栏透明

    1
    StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView)
  6. 在 Fragment 中使用

  7. 为滑动返回界面设置状态栏颜色

    推荐配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑动返回 这个库一起使用。

    1
    StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)
  8. 通过传入 statusBarAlpha 参数,可以改变状态栏的透明度值,默认值是112。

使用

  1. 在 build.gradle 文件中添加依赖, StatusBarUtil 已经发布在 JCenter:

    1
    compile 'com.jaeger.statusbarutil:library:1.4.0'
  2. setContentView() 之后调用你需要的方法,例如:

    1
    2
    3
    setContentView(R.layout.main_activity);
    ...
    StatusBarUtil.setColor(MainActivity.this, mColor);
  3. 如果你在一个包含 DrawerLayout 的界面中使用, 你需要在布局文件中为 DrawerLayout 添加 android:fitsSystemWindows="true" 属性:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    ...

    </android.support.v4.widget.DrawerLayout>
  4. 滑动返回界面设置状态栏颜色:

    建议配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑动返回 库一起使用。

    1
    StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)
  5. 当你设置了 statusBarAlpha 值时,该值需要在 0 ~ 255 之间

  6. 在 Fragment 中的使用可以参照 UseInFragmentActivity.java 来实现

SystemBarTintManager状态栏透明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import java.lang.reflect.Method;

public class SystemBarTintManager {
public static final int DEFAULT_TINT_COLOR = -1728053248;
private static String sNavBarOverride;
private final SystemBarTintManager.SystemBarConfig mConfig;
private boolean mStatusBarAvailable;
private boolean mNavBarAvailable;
private boolean mStatusBarTintEnabled;
private boolean mNavBarTintEnabled;
private View mStatusBarTintView;
private View mNavBarTintView;

@TargetApi(19)
public SystemBarTintManager(Activity activity) {
Window win = activity.getWindow();
ViewGroup decorViewGroup = (ViewGroup)win.getDecorView();
if(Build.VERSION.SDK_INT >= 19) {
int[] attrs = new int[]{16843759, 16843760};
TypedArray a = activity.obtainStyledAttributes(attrs);

try {
this.mStatusBarAvailable = a.getBoolean(0, false);
this.mNavBarAvailable = a.getBoolean(1, false);
} finally {
a.recycle();
}

WindowManager.LayoutParams winParams = win.getAttributes();
int bits = 67108864;
if((winParams.flags & bits) != 0) {
this.mStatusBarAvailable = true;
}

bits = 134217728;
if((winParams.flags & bits) != 0) {
this.mNavBarAvailable = true;
}
}

this.mConfig = new SystemBarTintManager.SystemBarConfig(activity, this.mStatusBarAvailable, this.mNavBarAvailable);
if(!this.mConfig.hasNavigtionBar()) {
this.mNavBarAvailable = false;
}

if(this.mStatusBarAvailable) {
this.setupStatusBarView(activity, decorViewGroup);
}

if(this.mNavBarAvailable) {
this.setupNavBarView(activity, decorViewGroup);
}

}

public void setStatusBarTintEnabled(boolean enabled) {
this.mStatusBarTintEnabled = enabled;
if(this.mStatusBarAvailable) {
this.mStatusBarTintView.setVisibility(enabled?View.VISIBLE:View.GONE);
}

}

public void setNavigationBarTintEnabled(boolean enabled) {
this.mNavBarTintEnabled = enabled;
if(this.mNavBarAvailable) {
this.mNavBarTintView.setVisibility(enabled?View.VISIBLE:View.GONE);
}

}

public void setTintColor(int color) {
this.setStatusBarTintColor(color);
this.setNavigationBarTintColor(color);
}

public void setTintResource(int res) {
this.setStatusBarTintResource(res);
this.setNavigationBarTintResource(res);
}

public void setTintDrawable(Drawable drawable) {
this.setStatusBarTintDrawable(drawable);
this.setNavigationBarTintDrawable(drawable);
}

public void setTintAlpha(float alpha) {
this.setStatusBarAlpha(alpha);
this.setNavigationBarAlpha(alpha);
}

public void setStatusBarTintColor(int color) {
if(this.mStatusBarAvailable) {
this.mStatusBarTintView.setBackgroundColor(color);
}

}

public void setStatusBarTintResource(int res) {
if(this.mStatusBarAvailable) {
this.mStatusBarTintView.setBackgroundResource(res);
}

}

public void setStatusBarTintDrawable(Drawable drawable) {
if(this.mStatusBarAvailable) {
this.mStatusBarTintView.setBackgroundDrawable(drawable);
}

}

@TargetApi(11)
public void setStatusBarAlpha(float alpha) {
if(this.mStatusBarAvailable && Build.VERSION.SDK_INT >= 11) {
this.mStatusBarTintView.setAlpha(alpha);
}

}

public void setNavigationBarTintColor(int color) {
if(this.mNavBarAvailable) {
this.mNavBarTintView.setBackgroundColor(color);
}

}

public void setNavigationBarTintResource(int res) {
if(this.mNavBarAvailable) {
this.mNavBarTintView.setBackgroundResource(res);
}

}

public void setNavigationBarTintDrawable(Drawable drawable) {
if(this.mNavBarAvailable) {
this.mNavBarTintView.setBackgroundDrawable(drawable);
}

}

@TargetApi(11)
public void setNavigationBarAlpha(float alpha) {
if(this.mNavBarAvailable && Build.VERSION.SDK_INT >= 11) {
this.mNavBarTintView.setAlpha(alpha);
}

}

public SystemBarTintManager.SystemBarConfig getConfig() {
return this.mConfig;
}

public boolean isStatusBarTintEnabled() {
return this.mStatusBarTintEnabled;
}

public boolean isNavBarTintEnabled() {
return this.mNavBarTintEnabled;
}

private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
this.mStatusBarTintView = new View(context);
android.widget.FrameLayout.LayoutParams params = new android.widget.FrameLayout.LayoutParams(-1, this.mConfig.getStatusBarHeight());
params.gravity = 48;
if(this.mNavBarAvailable && !this.mConfig.isNavigationAtBottom()) {
params.rightMargin = this.mConfig.getNavigationBarWidth();
}

this.mStatusBarTintView.setLayoutParams(params);
this.mStatusBarTintView.setBackgroundColor(-1728053248);
this.mStatusBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(this.mStatusBarTintView);
}

private void setupNavBarView(Context context, ViewGroup decorViewGroup) {
this.mNavBarTintView = new View(context);
android.widget.FrameLayout.LayoutParams params;
if(this.mConfig.isNavigationAtBottom()) {
params = new android.widget.FrameLayout.LayoutParams(-1, this.mConfig.getNavigationBarHeight());
params.gravity = 80;
} else {
params = new android.widget.FrameLayout.LayoutParams(this.mConfig.getNavigationBarWidth(), -1);
params.gravity = 5;
}

this.mNavBarTintView.setLayoutParams(params);
this.mNavBarTintView.setBackgroundColor(-1728053248);
this.mNavBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(this.mNavBarTintView);
}

static {
if(Build.VERSION.SDK_INT >= 19) {
try {
Class c = Class.forName("android.os.SystemProperties");
Method m = c.getDeclaredMethod("get", new Class[]{String.class});
m.setAccessible(true);
sNavBarOverride = (String)m.invoke((Object)null, new Object[]{"qemu.hw.mainkeys"});
} catch (Throwable var2) {
sNavBarOverride = null;
}
}

}

public static class SystemBarConfig {
private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";
private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height";
private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape";
private static final String NAV_BAR_WIDTH_RES_NAME = "navigation_bar_width";
private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";
private final boolean mTranslucentStatusBar;
private final boolean mTranslucentNavBar;
private final int mStatusBarHeight;
private final int mActionBarHeight;
private final boolean mHasNavigationBar;
private final int mNavigationBarHeight;
private final int mNavigationBarWidth;
private final boolean mInPortrait;
private final float mSmallestWidthDp;

private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
this.mInPortrait = res.getConfiguration().orientation == 1;
this.mSmallestWidthDp = this.getSmallestWidthDp(activity);
this.mStatusBarHeight = this.getInternalDimensionSize(res, "status_bar_height");
this.mActionBarHeight = this.getActionBarHeight(activity);
this.mNavigationBarHeight = this.getNavigationBarHeight(activity);
this.mNavigationBarWidth = this.getNavigationBarWidth(activity);
this.mHasNavigationBar = this.mNavigationBarHeight > 0;
this.mTranslucentStatusBar = translucentStatusBar;
this.mTranslucentNavBar = traslucentNavBar;
}

@TargetApi(14)
private int getActionBarHeight(Context context) {
int result = 0;
if(Build.VERSION.SDK_INT >= 14) {
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(16843499, tv, true);
result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}

return result;
}

@TargetApi(14)
private int getNavigationBarHeight(Context context) {
Resources res = context.getResources();
int result = 0;
if(Build.VERSION.SDK_INT >= 14 && this.hasNavBar(context)) {
String key;
if(this.mInPortrait) {
key = "navigation_bar_height";
} else {
key = "navigation_bar_height_landscape";
}

return this.getInternalDimensionSize(res, key);
} else {
return result;
}
}

@TargetApi(14)
private int getNavigationBarWidth(Context context) {
Resources res = context.getResources();
int result = 0;
return Build.VERSION.SDK_INT >= 14 && this.hasNavBar(context)?this.getInternalDimensionSize(res, "navigation_bar_width"):result;
}

@TargetApi(14)
private boolean hasNavBar(Context context) {
Resources res = context.getResources();
int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
if(resourceId != 0) {
boolean hasNav = res.getBoolean(resourceId);
if("1".equals(SystemBarTintManager.sNavBarOverride)) {
hasNav = false;
} else if("0".equals(SystemBarTintManager.sNavBarOverride)) {
hasNav = true;
}

return hasNav;
} else {
return !ViewConfiguration.get(context).hasPermanentMenuKey();
}
}

private int getInternalDimensionSize(Resources res, String key) {
int result = 0;
int resourceId = res.getIdentifier(key, "dimen", "android");
if(resourceId > 0) {
result = res.getDimensionPixelSize(resourceId);
}

return result;
}

@SuppressLint({"NewApi"})
private float getSmallestWidthDp(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
if(Build.VERSION.SDK_INT >= 16) {
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
} else {
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}

float widthDp = (float)metrics.widthPixels / metrics.density;
float heightDp = (float)metrics.heightPixels / metrics.density;
return Math.min(widthDp, heightDp);
}

public boolean isNavigationAtBottom() {
return this.mSmallestWidthDp >= 600.0F || this.mInPortrait;
}

public int getStatusBarHeight() {
return this.mStatusBarHeight;
}

public int getActionBarHeight() {
return this.mActionBarHeight;
}

public boolean hasNavigtionBar() {
return this.mHasNavigationBar;
}

public int getNavigationBarHeight() {
return this.mNavigationBarHeight;
}

public int getNavigationBarWidth() {
return this.mNavigationBarWidth;
}

public int getPixelInsetTop(boolean withActionBar) {
return (this.mTranslucentStatusBar?this.mStatusBarHeight:0) + (withActionBar?this.mActionBarHeight:0);
}

public int getPixelInsetBottom() {
return this.mTranslucentNavBar && this.isNavigationAtBottom()?this.mNavigationBarHeight:0;
}

public int getPixelInsetRight() {
return this.mTranslucentNavBar && !this.isNavigationAtBottom()?this.mNavigationBarWidth:0;
}
}
}

在基类中 onCreate()中调用 setStatusBar();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
protected void setStatusBar(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
setTranslucentStatus(true);
}

SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setNavigationBarTintEnabled(true);
tintManager.setTintColor(Color.TRANSPARENT);
tintManager.setNavigationBarTintColor(Color.BLACK);//通知栏所需颜色
}

@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on){
winParams.flags |= bits;
}else {
winParams.flags &= ~bits;
}
window.setAttributes(winParams);
}