Skip to content

getglory/StatusBarCompat

 
 

Repository files navigation

StatusBarCompat

Android Arsenal

Only work for SDK >= 19.

Demo Download

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

Add the dependency

dependencies {
        compile 'com.github.niorgai:StatusBarCompat:1.1.0'
}
SDK KITKAT(19) LOLLIPOP(21)
TranslucentStatusBar SetStatusBarColor

This is a utils for status bar, And you do not need to change your style.xml file. The most feature is you can toggle setStatusBarColor and translucentStatusBar without recreate activity.

//set color for status bar
StatusBarCompat.setStatusBarColor(Activity activity, int color)
//add alpha to color
StatusBarCompat.setStatusBarColor(Activity activity, int statusColor, int alpha)

//translucent status bar
StatusBarCompat.translucentStatusBar(activity);
//should hide status bar background (default black background) when SDK >= 21
StatusBarCompat.translucentStatusBar(Activity activity, boolean hideStatusBarBackground);

How to use

  1. Use it after call setContentView
  2. If use FullScreen Activity, must call StatusBarCompat.translucentStatusBar(activity);

About Demo:

  1. Both FloatingActionButton can toggle between setStatusBarColor and translucentStatusBar.
  2. First Button will jump to an Activity which is use StatusBarCompat, It work perfect.
  3. Second Button will jump to an Activity which is use StatusBarCompat1, It has a black line below statusBar when you call setStatusBarColor In KitKat.
  4. Third Button will jump to an Activity which is use StatusBarCompat2 and call setStatusBarColor. But it will not work if you call translucentStatusBar after In KitKat.
  5. Fourth Button will jump to an Activity which is use StatusBarCompat2 and call translucentStatusBar. But it will not work if you call setStatusBarColor after In KitKat.

Issues:

These porblem only show in KIKKAT:

  1. If work with TabActivity, It will show a black line in contentView, so you can call StatusBarCompat.setStatusBarColor(Activity activity, int statusColor, int alpha) and the suggest alpha is 112.
  2. If first view is DrawerLayout, let its child view set fitsSystemWindow to false.

Link:

  1. I had show three type to set statusBar, only the class StatusBarCompat is suggested, But you can see their different in my blog Android-transulcent-status-bar.

中文版

透明状态栏只有在 SDK >= 19 (Android 4.4) 才会生效.

透明状态栏 设置状态栏的颜色
)

这是一个为了兼容处理状态栏的工具类,可以不需要设置不同的style.xml文件,最重要的特性就是可以在**不重启 Activity 的情况下切换 setStatusBarColortranslucentStatusBar **. 提供以下 API :

//设置状态栏的颜色
StatusBarCompat.setStatusBarColor(Activity activity)
StatusBarCompat.setStatusBarColor(Activity activity, int color)
//为状态栏颜色设置透明度
StatusBarCompat.setStatusBarColor(Activity activity, int statusColor, int alpha)

//透明状态栏
StatusBarCompat.translucentStatusBar(activity);
//当SDK >= 21时, 是否需要隐藏状态栏的背景色(默认的黑色背景)
StatusBarCompat.translucentStatusBar(Activity activity, boolean hideStatusBarBackground);

怎么使用

  1. setContentView 方法调用后再设置.
  2. 如果使用了全屏 Activity ,记得调用StatusBarCompat.translucentStatusBar(activity);

Demo 说明:

  1. 每一个 FloatingActionButton 可以在 setStatusBarColortranslucentStatusBar 中切换.
  2. 第一个按钮跳转的 Activity 使用 StatusBarCompat, 效果完美.
  3. 第二个按钮跳转的 Activity 使用 StatusBarCompat1,在 Android 4.4-5.0 ,如果调用了 setStatusBarColor. 状态栏下方会有一条黑线.
  4. 第三个按钮跳转的 Activity 使用 StatusBarCompat2 并且调用 setStatusBarColor. 但是在 Android 4.4-5.0 , 如果接下来调用 translucentStatusBar 会无效.
  5. 第四个按钮跳转的 Activity 使用 StatusBarCompat2 并且调用 translucentStatusBar. 但是在 Android 4.4-5.0 , 如果接下来调用 setStatusBarColor 会无效.
  6. 这里提供了 StatusBarCompat , StatusBarCompat1 , StatusBarCompat2 类,推荐使用StatusBarCompat类, 其他两个类的实现方式都有一些小问题.

已知问题

这些问题仅仅出现在 4.4 上

  1. 如果用在 TabActivity 上, 会有一条黑线在状态栏下面, 推荐使用 StatusBarCompat.setStatusBarColor(Activity activity, int statusColor, int alpha) 方法, 推荐的透明值为 112.
  2. 如果 layout 中第一个 View 为 DrawerLayout, 那么它的子 View 的 fitsSystemWindow 需要设置为 false.

更多

在我的博客Android-transulcent-status-bar中可以查看更多细节.

License

The MIT License (MIT)

Copyright (c) 2016 JQ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Status Bar Utils ---- Change Status Bar Mode Simply

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%