沉浸式状态栏颜色设置,对android4.4~6.0的总结
来源:互联网 发布:java常用工具 编辑:程序博客网 时间:2024/05/01 05:00
好吧,鸿洋大神已经讲得非常清楚了
http://blog.csdn.net/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; }
1 0
- 沉浸式状态栏颜色设置,对android4.4~6.0的总结
- android4.4沉浸式状态栏总结
- Android设置沉浸式状态栏时改变状态栏的颜色(只对MIUI V6可用)
- Android状态栏颜色设置(沉浸式状态栏)
- Android 设置状态栏颜色&&沉浸式状态栏
- 沉浸式状态栏 设置状态栏颜色
- android沉浸式 & 状态栏颜色设置
- Android沉浸式(设置状态栏颜色)
- 沉浸式状态栏(Android4.4及以上)
- Android4.4 5.0沉浸式状态栏
- Android4.4以上沉浸式状态栏
- Android4.4以上实现沉浸式状态栏
- Android4.4 实现沉浸式状态栏
- 沉浸式状态栏的设置
- 设置Android系统的状态栏颜色(从Android4.4开始)
- android设置状态栏颜色(沉浸式状态栏)
- Android 沉浸式状态栏 一设置状态栏颜色
- Android,沉浸式状态栏,状态栏以及Toolbar颜色分开设置
- 使用WinInet访问没有证书的Https (GET/POST)
- spring mvc常用的注解
- 冒泡排序
- Measure of similarity
- Android高德地图使用
- 沉浸式状态栏颜色设置,对android4.4~6.0的总结
- 讯飞语音听写开发入门之获取appid号
- C++第七次上机实验
- 4.3 概率判别式模型
- 剑指offer-28字符串的排列
- 2015级C++第15周程序阅读 范型程序设计
- static const extern在C与C++中的区别
- 每日一得--关于Integer大小比较的问题
- 码表dashboard仪表盘自定义View的开发