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