24 | * Email: chjie.jaeger@gmail.com 25 | * GitHub: https://github.com/laobie 26 | */ 27 | public class StatusBarUtil { 28 | 29 | public static final int DEFAULT_STATUS_BAR_ALPHA = 112; 30 | private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view; 31 | private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view; 32 | private static final int TAG_KEY_HAVE_SET_OFFSET = -123; 33 | 34 | /** 35 | * 设置状态栏颜色 36 | * 37 | * @param activity 需要设置的 activity 38 | * @param color 状态栏颜色值 39 | */ 40 | public static void setColor(Activity activity, @ColorInt int color) { 41 | setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA); 42 | } 43 | 44 | /** 45 | * 设置状态栏颜色 46 | * 47 | * @param activity 需要设置的activity 48 | * @param color 状态栏颜色值 49 | * @param statusBarAlpha 状态栏透明度 50 | */ 51 | 52 | public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { 53 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 54 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 55 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 56 | activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); 57 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 58 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 59 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); 60 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); 61 | if (fakeStatusBarView != null) { 62 | if (fakeStatusBarView.getVisibility() == View.GONE) { 63 | fakeStatusBarView.setVisibility(View.VISIBLE); 64 | } 65 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 66 | } else { 67 | decorView.addView(createStatusBarView(activity, color, statusBarAlpha)); 68 | } 69 | setRootView(activity); 70 | } 71 | } 72 | 73 | /** 74 | * 为滑动返回界面设置状态栏颜色 75 | * 76 | * @param activity 需要设置的activity 77 | * @param color 状态栏颜色值 78 | */ 79 | public static void setColorForSwipeBack(Activity activity, int color) { 80 | setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA); 81 | } 82 | 83 | /** 84 | * 为滑动返回界面设置状态栏颜色 85 | * 86 | * @param activity 需要设置的activity 87 | * @param color 状态栏颜色值 88 | * @param statusBarAlpha 状态栏透明度 89 | */ 90 | public static void setColorForSwipeBack(Activity activity, @ColorInt int color, 91 | @IntRange(from = 0, to = 255) int statusBarAlpha) { 92 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 93 | 94 | ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content)); 95 | View rootView = contentView.getChildAt(0); 96 | int statusBarHeight = getStatusBarHeight(activity); 97 | if (rootView != null && rootView instanceof CoordinatorLayout) { 98 | final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView; 99 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 100 | coordinatorLayout.setFitsSystemWindows(false); 101 | contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 102 | boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight; 103 | if (isNeedRequestLayout) { 104 | contentView.setPadding(0, statusBarHeight, 0, 0); 105 | coordinatorLayout.post(new Runnable() { 106 | @Override 107 | public void run() { 108 | coordinatorLayout.requestLayout(); 109 | } 110 | }); 111 | } 112 | } else { 113 | coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 114 | } 115 | } else { 116 | contentView.setPadding(0, statusBarHeight, 0, 0); 117 | contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 118 | } 119 | setTransparentForWindow(activity); 120 | } 121 | } 122 | 123 | /** 124 | * 设置状态栏纯色 不加半透明效果 125 | * 126 | * @param activity 需要设置的 activity 127 | * @param color 状态栏颜色值 128 | */ 129 | public static void setColorNoTranslucent(Activity activity, @ColorInt int color) { 130 | setColor(activity, color, 0); 131 | } 132 | 133 | /** 134 | * 设置状态栏颜色(5.0以下无半透明效果,不建议使用) 135 | * 136 | * @param activity 需要设置的 activity 137 | * @param color 状态栏颜色值 138 | */ 139 | @Deprecated 140 | public static void setColorDiff(Activity activity, @ColorInt int color) { 141 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 142 | return; 143 | } 144 | transparentStatusBar(activity); 145 | ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); 146 | // 移除半透明矩形,以免叠加 147 | View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID); 148 | if (fakeStatusBarView != null) { 149 | if (fakeStatusBarView.getVisibility() == View.GONE) { 150 | fakeStatusBarView.setVisibility(View.VISIBLE); 151 | } 152 | fakeStatusBarView.setBackgroundColor(color); 153 | } else { 154 | contentView.addView(createStatusBarView(activity, color)); 155 | } 156 | setRootView(activity); 157 | } 158 | 159 | /** 160 | * 使状态栏半透明 161 | *
162 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 163 | * 164 | * @param activity 需要设置的activity 165 | */ 166 | public static void setTranslucent(Activity activity) { 167 | setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA); 168 | } 169 | 170 | /** 171 | * 使状态栏半透明 172 | *
173 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 174 | * 175 | * @param activity 需要设置的activity 176 | * @param statusBarAlpha 状态栏透明度 177 | */ 178 | public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { 179 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 180 | return; 181 | } 182 | setTransparent(activity); 183 | addTranslucentView(activity, statusBarAlpha); 184 | } 185 | 186 | /** 187 | * 针对根布局是 CoordinatorLayout, 使状态栏半透明 188 | *
189 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 190 | * 191 | * @param activity 需要设置的activity 192 | * @param statusBarAlpha 状态栏透明度 193 | */ 194 | public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { 195 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 196 | return; 197 | } 198 | transparentStatusBar(activity); 199 | addTranslucentView(activity, statusBarAlpha); 200 | } 201 | 202 | /** 203 | * 设置状态栏全透明 204 | * 205 | * @param activity 需要设置的activity 206 | */ 207 | public static void setTransparent(Activity activity) { 208 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 209 | return; 210 | } 211 | transparentStatusBar(activity); 212 | setRootView(activity); 213 | } 214 | 215 | /** 216 | * 使状态栏透明(5.0以上半透明效果,不建议使用) 217 | *
218 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏
219 | *
220 | * @param activity 需要设置的activity
221 | */
222 | @Deprecated
223 | public static void setTranslucentDiff(Activity activity) {
224 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
225 | // 设置状态栏透明
226 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
227 | setRootView(activity);
228 | }
229 | }
230 |
231 | /**
232 | * 为DrawerLayout 布局设置状态栏变色
233 | *
234 | * @param activity 需要设置的activity
235 | * @param drawerLayout DrawerLayout
236 | * @param color 状态栏颜色值
237 | */
238 | public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
239 | setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA);
240 | }
241 |
242 | /**
243 | * 为DrawerLayout 布局设置状态栏颜色,纯色
244 | *
245 | * @param activity 需要设置的activity
246 | * @param drawerLayout DrawerLayout
247 | * @param color 状态栏颜色值
248 | */
249 | public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
250 | setColorForDrawerLayout(activity, drawerLayout, color, 0);
251 | }
252 |
253 | /**
254 | * 为DrawerLayout 布局设置状态栏变色
255 | *
256 | * @param activity 需要设置的activity
257 | * @param drawerLayout DrawerLayout
258 | * @param color 状态栏颜色值
259 | * @param statusBarAlpha 状态栏透明度
260 | */
261 | public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color,
262 | @IntRange(from = 0, to = 255) int statusBarAlpha) {
263 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
264 | return;
265 | }
266 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
267 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
268 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
269 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
270 | } else {
271 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
272 | }
273 | // 生成一个状态栏大小的矩形
274 | // 添加 statusBarView 到布局中
275 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
276 | View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
277 | if (fakeStatusBarView != null) {
278 | if (fakeStatusBarView.getVisibility() == View.GONE) {
279 | fakeStatusBarView.setVisibility(View.VISIBLE);
280 | }
281 | fakeStatusBarView.setBackgroundColor(color);
282 | } else {
283 | contentLayout.addView(createStatusBarView(activity, color), 0);
284 | }
285 | // 内容布局不是 LinearLayout 时,设置padding top
286 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
287 | contentLayout.getChildAt(1)
288 | .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
289 | contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
290 | }
291 | // 设置属性
292 | setDrawerLayoutProperty(drawerLayout, contentLayout);
293 | addTranslucentView(activity, statusBarAlpha);
294 | }
295 |
296 | /**
297 | * 设置 DrawerLayout 属性
298 | *
299 | * @param drawerLayout DrawerLayout
300 | * @param drawerLayoutContentLayout DrawerLayout 的内容布局
301 | */
302 | private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) {
303 | ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
304 | drawerLayout.setFitsSystemWindows(false);
305 | drawerLayoutContentLayout.setFitsSystemWindows(false);
306 | drawerLayoutContentLayout.setClipToPadding(true);
307 | drawer.setFitsSystemWindows(false);
308 | }
309 |
310 | /**
311 | * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
312 | *
313 | * @param activity 需要设置的activity
314 | * @param drawerLayout DrawerLayout
315 | * @param color 状态栏颜色值
316 | */
317 | @Deprecated
318 | public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
319 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
320 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
321 | // 生成一个状态栏大小的矩形
322 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
323 | View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
324 | if (fakeStatusBarView != null) {
325 | if (fakeStatusBarView.getVisibility() == View.GONE) {
326 | fakeStatusBarView.setVisibility(View.VISIBLE);
327 | }
328 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA));
329 | } else {
330 | // 添加 statusBarView 到布局中
331 | contentLayout.addView(createStatusBarView(activity, color), 0);
332 | }
333 | // 内容布局不是 LinearLayout 时,设置padding top
334 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
335 | contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
336 | }
337 | // 设置属性
338 | setDrawerLayoutProperty(drawerLayout, contentLayout);
339 | }
340 | }
341 |
342 | /**
343 | * 为 DrawerLayout 布局设置状态栏透明
344 | *
345 | * @param activity 需要设置的activity
346 | * @param drawerLayout DrawerLayout
347 | */
348 | public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
349 | setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA);
350 | }
351 |
352 | /**
353 | * 为 DrawerLayout 布局设置状态栏透明
354 | *
355 | * @param activity 需要设置的activity
356 | * @param drawerLayout DrawerLayout
357 | */
358 | public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout,
359 | @IntRange(from = 0, to = 255) int statusBarAlpha) {
360 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
361 | return;
362 | }
363 | setTransparentForDrawerLayout(activity, drawerLayout);
364 | addTranslucentView(activity, statusBarAlpha);
365 | }
366 |
367 | /**
368 | * 为 DrawerLayout 布局设置状态栏透明
369 | *
370 | * @param activity 需要设置的activity
371 | * @param drawerLayout DrawerLayout
372 | */
373 | public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
374 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
375 | return;
376 | }
377 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
378 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
379 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
380 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
381 | } else {
382 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
383 | }
384 |
385 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
386 | // 内容布局不是 LinearLayout 时,设置padding top
387 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
388 | contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
389 | }
390 |
391 | // 设置属性
392 | setDrawerLayoutProperty(drawerLayout, contentLayout);
393 | }
394 |
395 | /**
396 | * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用)
397 | *
398 | * @param activity 需要设置的activity
399 | * @param drawerLayout DrawerLayout
400 | */
401 | @Deprecated
402 | public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) {
403 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
404 | // 设置状态栏透明
405 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
406 | // 设置内容布局属性
407 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
408 | contentLayout.setFitsSystemWindows(true);
409 | contentLayout.setClipToPadding(true);
410 | // 设置抽屉布局属性
411 | ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
412 | vg.setFitsSystemWindows(false);
413 | // 设置 DrawerLayout 属性
414 | drawerLayout.setFitsSystemWindows(false);
415 | }
416 | }
417 |
418 | /**
419 | * 为头部是 ImageView 的界面设置状态栏全透明
420 | *
421 | * @param activity 需要设置的activity
422 | * @param needOffsetView 需要向下偏移的 View
423 | */
424 | public static void setTransparentForImageView(Activity activity, View needOffsetView) {
425 | setTranslucentForImageView(activity, 0, needOffsetView);
426 | }
427 |
428 | /**
429 | * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度)
430 | *
431 | * @param activity 需要设置的activity
432 | * @param needOffsetView 需要向下偏移的 View
433 | */
434 | public static void setTranslucentForImageView(Activity activity, View needOffsetView) {
435 | setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
436 | }
437 |
438 | /**
439 | * 为头部是 ImageView 的界面设置状态栏透明
440 | *
441 | * @param activity 需要设置的activity
442 | * @param statusBarAlpha 状态栏透明度
443 | * @param needOffsetView 需要向下偏移的 View
444 | */
445 | public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
446 | View needOffsetView) {
447 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
448 | return;
449 | }
450 | setTransparentForWindow(activity);
451 | addTranslucentView(activity, statusBarAlpha);
452 | if (needOffsetView != null) {
453 | Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET);
454 | if (haveSetOffset != null && (Boolean) haveSetOffset) {
455 | return;
456 | }
457 | ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
458 | layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
459 | layoutParams.rightMargin, layoutParams.bottomMargin);
460 | needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);
461 | }
462 | }
463 |
464 | /**
465 | * 为 fragment 头部是 ImageView 的设置状态栏透明
466 | *
467 | * @param activity fragment 对应的 activity
468 | * @param needOffsetView 需要向下偏移的 View
469 | */
470 | public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) {
471 | setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
472 | }
473 |
474 | /**
475 | * 为 fragment 头部是 ImageView 的设置状态栏透明
476 | *
477 | * @param activity fragment 对应的 activity
478 | * @param needOffsetView 需要向下偏移的 View
479 | */
480 | public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) {
481 | setTranslucentForImageViewInFragment(activity, 0, needOffsetView);
482 | }
483 |
484 | /**
485 | * 为 fragment 头部是 ImageView 的设置状态栏透明
486 | *
487 | * @param activity fragment 对应的 activity
488 | * @param statusBarAlpha 状态栏透明度
489 | * @param needOffsetView 需要向下偏移的 View
490 | */
491 | public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
492 | View needOffsetView) {
493 | setTranslucentForImageView(activity, statusBarAlpha, needOffsetView);
494 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
495 | clearPreviousSetting(activity);
496 | }
497 | }
498 |
499 | /**
500 | * 隐藏伪状态栏 View
501 | *
502 | * @param activity 调用的 Activity
503 | */
504 | public static void hideFakeStatusBarView(Activity activity) {
505 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
506 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
507 | if (fakeStatusBarView != null) {
508 | fakeStatusBarView.setVisibility(View.GONE);
509 | }
510 | View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
511 | if (fakeTranslucentView != null) {
512 | fakeTranslucentView.setVisibility(View.GONE);
513 | }
514 | }
515 |
516 | @TargetApi(Build.VERSION_CODES.M)
517 | public static void setLightMode(Activity activity) {
518 | setMIUIStatusBarDarkIcon(activity, true);
519 | setMeizuStatusBarDarkIcon(activity, true);
520 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
521 | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
522 | }
523 | }
524 |
525 | @TargetApi(Build.VERSION_CODES.M)
526 | public static void setDarkMode(Activity activity) {
527 | setMIUIStatusBarDarkIcon(activity, false);
528 | setMeizuStatusBarDarkIcon(activity, false);
529 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
530 | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
531 | }
532 | }
533 |
534 | /**
535 | * 修改 MIUI V6 以上状态栏颜色
536 | */
537 | private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
538 | Class extends Window> clazz = activity.getWindow().getClass();
539 | try {
540 | Class> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
541 | Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
542 | int darkModeFlag = field.getInt(layoutParams);
543 | Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
544 | extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag);
545 | } catch (Exception e) {
546 | //e.printStackTrace();
547 | }
548 | }
549 |
550 | /**
551 | * 修改魅族状态栏字体颜色 Flyme 4.0
552 | */
553 | private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
554 | try {
555 | WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
556 | Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
557 | Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
558 | darkFlag.setAccessible(true);
559 | meizuFlags.setAccessible(true);
560 | int bit = darkFlag.getInt(null);
561 | int value = meizuFlags.getInt(lp);
562 | if (darkIcon) {
563 | value |= bit;
564 | } else {
565 | value &= ~bit;
566 | }
567 | meizuFlags.setInt(lp, value);
568 | activity.getWindow().setAttributes(lp);
569 | } catch (Exception e) {
570 | //e.printStackTrace();
571 | }
572 | }
573 |
574 | ///////////////////////////////////////////////////////////////////////////////////
575 |
576 | @TargetApi(Build.VERSION_CODES.KITKAT)
577 | private static void clearPreviousSetting(Activity activity) {
578 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
579 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
580 | if (fakeStatusBarView != null) {
581 | decorView.removeView(fakeStatusBarView);
582 | ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
583 | rootView.setPadding(0, 0, 0, 0);
584 | }
585 | }
586 |
587 | /**
588 | * 添加半透明矩形条
589 | *
590 | * @param activity 需要设置的 activity
591 | * @param statusBarAlpha 透明值
592 | */
593 | private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
594 | ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
595 | View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
596 | if (fakeTranslucentView != null) {
597 | if (fakeTranslucentView.getVisibility() == View.GONE) {
598 | fakeTranslucentView.setVisibility(View.VISIBLE);
599 | }
600 | fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
601 | } else {
602 | contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
603 | }
604 | }
605 |
606 | /**
607 | * 生成一个和状态栏大小相同的彩色矩形条
608 | *
609 | * @param activity 需要设置的 activity
610 | * @param color 状态栏颜色值
611 | * @return 状态栏矩形条
612 | */
613 | private static View createStatusBarView(Activity activity, @ColorInt int color) {
614 | return createStatusBarView(activity, color, 0);
615 | }
616 |
617 | /**
618 | * 生成一个和状态栏大小相同的半透明矩形条
619 | *
620 | * @param activity 需要设置的activity
621 | * @param color 状态栏颜色值
622 | * @param alpha 透明值
623 | * @return 状态栏矩形条
624 | */
625 | private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {
626 | // 绘制一个和状态栏一样高的矩形
627 | View statusBarView = new View(activity);
628 | LinearLayout.LayoutParams params =
629 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
630 | statusBarView.setLayoutParams(params);
631 | statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
632 | statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
633 | return statusBarView;
634 | }
635 |
636 | /**
637 | * 设置根布局参数
638 | */
639 | private static void setRootView(Activity activity) {
640 | ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
641 | for (int i = 0, count = parent.getChildCount(); i < count; i++) {
642 | View childView = parent.getChildAt(i);
643 | if (childView instanceof ViewGroup) {
644 | childView.setFitsSystemWindows(true);
645 | ((ViewGroup) childView).setClipToPadding(true);
646 | }
647 | }
648 | }
649 |
650 | /**
651 | * 设置透明
652 | */
653 | private static void setTransparentForWindow(Activity activity) {
654 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
655 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
656 | activity.getWindow()
657 | .getDecorView()
658 | .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
659 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
660 | activity.getWindow()
661 | .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
662 | }
663 | }
664 |
665 | /**
666 | * 使状态栏透明
667 | */
668 | @TargetApi(Build.VERSION_CODES.KITKAT)
669 | private static void transparentStatusBar(Activity activity) {
670 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
671 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
672 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
673 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
674 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
675 | } else {
676 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
677 | }
678 | }
679 |
680 | /**
681 | * 创建半透明矩形 View
682 | *
683 | * @param alpha 透明值
684 | * @return 半透明 View
685 | */
686 | private static View createTranslucentStatusBarView(Activity activity, int alpha) {
687 | // 绘制一个和状态栏一样高的矩形
688 | View statusBarView = new View(activity);
689 | LinearLayout.LayoutParams params =
690 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
691 | statusBarView.setLayoutParams(params);
692 | statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
693 | statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
694 | return statusBarView;
695 | }
696 |
697 | /**
698 | * 获取状态栏高度
699 | *
700 | * @param context context
701 | * @return 状态栏高度
702 | */
703 | private static int getStatusBarHeight(Context context) {
704 | // 获得状态栏高度
705 | int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
706 | return context.getResources().getDimensionPixelSize(resourceId);
707 | }
708 |
709 | /**
710 | * 计算状态栏颜色
711 | *
712 | * @param color color值
713 | * @param alpha alpha值
714 | * @return 最终的状态栏颜色
715 | */
716 | private static int calculateStatusColor(@ColorInt int color, int alpha) {
717 | if (alpha == 0) {
718 | return color;
719 | }
720 | float a = 1 - alpha / 255f;
721 | int red = color >> 16 & 0xff;
722 | int green = color >> 8 & 0xff;
723 | int blue = color & 0xff;
724 | red = (int) (red * a + 0.5);
725 | green = (int) (green * a + 0.5);
726 | blue = (int) (blue * a + 0.5);
727 | return 0xff << 24 | red << 16 | green << 8 | blue;
728 | }
729 | }
730 |
--------------------------------------------------------------------------------
/library/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |