做项目时,发现APP的状态栏是系统默认的颜色,突然想到,为啥别的APP是自己设置的颜色(和APP本身很相搭),于是也想给自己的APP设置系统状态栏的颜色,更加美美哒。。。
搜了下,发现原来设置状态栏居然有个很高大上的名字(听不懂的都是高大上)——沉浸式状态栏,Android4.4以后开始支持沉浸式状态栏, 继续搜索,发现,有一个很简单的开源项目——SystemBarTint,可以很完美的支持沉浸式状态栏。
SystemBarTint地址: https://github.com/hexiaochun/SystemBarTint
下面,简单演示下如何使用该库,首先,先看下效果,有图才有真相:
1. 引入类库
使用Android Studio,直接在build.gradle文件中引入库:
dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
}
使用Eclipse,可下载JAR包,并引入到项目的libs文件夹中。
2. 在Activity中添加方法:
/**
* Apply KitKat specific translucency.
*/
private void applyKitKatTranslucency() { // KitKat translucent navigation/status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true); mTintManager.setStatusBarTintResource(R.color.colorTop);//通知栏所需颜色
} } @TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
然后, 在OnCreate()方法中调用applyKitKatTranslucency方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); applyKitKatTranslucency();
}
3. 在style.xml中,添加系统的样式:
<!-- 去掉tab顶部的黑边 -->
<style name="no_title" parent="@android:style/Theme.Light.NoTitleBar"> <!-- 沉浸式状态栏 -->
<item name="android:fitsSystemWindows">true</item>
<item name="android:clipToPadding">false</item>
</style>
当然了,别忘了在AndroidManifest.xml进行配置主题:
<application
android:name=".activity.base.MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:persistent="true"
android:theme="@style/no_title">
</application>
注: 这个是必要的,如果不添加,会造成一些页面的变形。
综上, 便可以在4.4以上的系统中方便的设置状态栏颜色,有木有感觉你的APP变得更好看了呢!
扩展:http://www.jianshu.com/p/e1c937000343