沉浸式状态栏颜色设置,对android4.4~6.0的总结

好吧,鸿洋大神已经讲得非常清楚了
http://blog.youkuaiyun.com/lmj623565791/article/details/48649563

首先,我了解的状态栏颜色设置必须android4.4以及以上

5.0以上在AppCompartActivity利用Theme就可以实现了相同颜色,但是没有渐变效果

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>

还需要为activity的根布局添加一个属性android:fitsSystemWindows=”true”
效果图
这里写图片描述

不继承AppCompartActivit也可以,下面是Cordova中StatusBar插件利用反射的实现方案

private void setStatusBarBackgroundColor(final String colorPref) {
        if (Build.VERSION.SDK_INT >= 21) {
            if (colorPref != null && !colorPref.isEmpty()) {
                final Window window = cordova.getActivity().getWindow();
                // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
                //SDK19:WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.clearFlags(0x04000000);      //SDK21:WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.addFlags(0x80000000);
                try {
                    // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
                    window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
                } catch (IllegalArgumentException ignore) {
                    Log.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
                } catch (Exception ignore) {
                    // this should not happen, only in case Android removes this method in a version > 21
                    Log.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
                }
            }
        }
    }

这两种方案在4.4系统没有效果.

真正的沉浸式状态栏其实只能在4.4-5.0之间,实现方案这个SystemBarTintManager类

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < 21) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.colorPrimary));
            tintManager.setStatusBarTintEnabled(true);
        }

这种方式实现渐变式的状态栏背景,从黑色到colorPrimary.也需要为activity的根布局添加一个属性android:fitsSystemWindows=”true” 效果:
这里写图片描述

但是在5.0以上的表现却不是渐变式,而是直接颜色差异了,like this:
这里写图片描述

所以需要在代码里面设定了SDK版本<21

SystemBarTintManager这个类是网上抄来的,出处忘记了.https://github.com/jgilfelt/SystemBarTint的更完善.

应用在Cordova的页面的时候,如果专为他打造4.4的沉浸式状态栏,还会发现,无法给他设置fitsSystemWindows=true,在代码里面直接设置也无效,导致标题栏被部分遮挡
最终解决办法如下

 //由于设置fitSystemWindow无效,设置一个类似fitSystemWindow的效果,否则content会从最顶部开始
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < 21) {
            //由于设置fitSystemWindow无效,设置一个类似fitSystemWindow的效果,否则content会从最顶部开始
            FrameLayout.LayoutParams p = (FrameLayout.LayoutParams) appView.getView().getLayoutParams();
            p.topMargin = getStatusBarHeight();

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.colorPrimary));
            tintManager.setStatusBarTintEnabled(true);
        }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值