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