下面介绍的是关于一个开源库的运用设置,开源库的地址为
https://github.com/jgilfelt/SystemBarTint
只需要添加依赖:
<span style="font-size:14px;">'com.readystatesoftware.systembartint:systembartint:1.0.3'</span>
参考以下的博客,总结的设置方法
http://www.jianshu.com/p/0acc12c29c1b
开源库的运用地址http://www.w2bc.com/Article/22050
改变状态栏颜色方法
<span style="font-size:14px;">public void changeStatusColor(){
SystemBarTintManager tintManager = new SystemBarTintManager(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
// enable status bar tint
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint
tintManager.setNavigationBarTintEnabled(true);
tintManager.setTintColor(Color.parseColor("#ffff4444"));
}</span>
<span style="font-size:14px;"> @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);
}</span>
当我再xml布局文件中加入属性(这个属性很重要,必须要加上,当然最好可以加入style.xml中当做主题给activity)
<span style="font-size:14px;">android:fitsSystemWindows="true"</span>
时,状态栏颜色改变了,但是问题来了,这个方法是相当于整体将title和内容向上移动,导致状态栏的高度没有了找了一下原因,发现在资源文件中加入values-v19和values-21这两个文件夹下的styles.xml就可以正常显示了
v19中的styles.xml
<span style="font-size:14px;"><resources>
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item> <span style="font-family: Arial, Helvetica, sans-serif;">//此属性决定能否正常显示</span>
</style>
</resources></span>
v21中的styles.xml
<span style="font-size:14px;"><resources >
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item> //此属性决定能否正常显示
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/holo_red_light</item>
</style>
</resources></span>
此时只需要将activity的主题改变就可以了
<span style="font-size:14px;"><activity android:name=".MainActivity"
android:theme="@style/ColorTranslucentTheme"/></span>
还有就是可以自己写出这个效果,参考地址
http://blog.csdn.net/jdsjlzx/article/details/41643587
作者写的很好,但是当我调用int statusBarHeight = getStatusBarHeight(activity); 方法时,系统报错找不到这个方法,不知道什么原因,希望知道的童鞋能够与我交流,非常感谢