关于5.0以下Android状态栏背景色变换

下面介绍的是关于一个开源库的运用设置,开源库的地址为

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


最初的时候直接按照开源库的运用方法,发现并不能达到所想要的效果(我所用的是小米4.4版本的手机)

改变状态栏颜色方法

<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); 方法时,系统报错找不到这个方法,不知道什么原因,希望知道的童鞋能够与我交流,非常感谢


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值