android状态栏颜色修改


 


状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的。


 


5.x环境下


方式一,状态栏将显示为纯净的颜色,没有渐变效果

/**        


                   * 状态栏相关工具类        


                   *        


                   */        


          public          class          StatusBarUtils {        


                   


                   public          static          void          setWindowStatusBarColor(Activity activity,          int          colorResId) {        


                   try          {        


                   if          (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        


                   Window window = activity.getWindow();        


                   window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);        


                   window.setStatusBarColor(activity.getResources().getColor(colorResId));        


                   


                   //底部导航栏        


                   //window.setNavigationBarColor(activity.getResources().getColor(colorResId));        


                   }        


                   }          catch          (Exception e) {        


                   e.printStackTrace();        


                   }        


                   }        


                   


                   public          static          void          setWindowStatusBarColor(Dialog dialog,          int          colorResId) {        


                   try          {        


                   if          (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        


                   Window window = dialog.getWindow();        


                   window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);        


                   window.setStatusBarColor(dialog.getContext().getResources().getColor(colorResId));        


                   


                   //底部导航栏        


                   //window.setNavigationBarColor(activity.getResources().getColor(colorResId));        


                   }        


                   }          catch          (Exception e) {        


                   e.printStackTrace();        


                   }        


                   }        


          }



  


 在这个setContentView之前调用



StatusBarUtils.setWindowStatusBarColor(this,R.color.main_btn_color_normal);

        setContentView(R.layout.activity_cpu_cooling);





android .R.color. holo_blue_bright,标题栏颜色可以和状态栏一样, EditText的输入也没有受影响




android状态栏颜色修改_android

 


android状态栏颜色修改_android_02

ps: 如果顶部为渐变效果,可能是在主题中设置

windowTranslucentStatus=true

属性。

 


方式二:


通过Style来修改状态栏颜色。


1.设置 colorPrimary,colorPrimaryDark两个颜色。

<          stylename          ="AppTheme"parent="Theme.AppCompat.Light.NoActionBar">         

                     <          itemname          ="colorPrimary">@android:color/holo_blue_bright</          item          >         

                     <          itemname          ="colorPrimaryDark">@android:color/holo_blue_bright</          item          >         

           </          style          >


2. AndroidManifest.xml文件中的targetSdkVersion必须设置在 21以上。


3.parent主题必须是 Theme.AppCompat开头,兼容包下的主题,所以必须一用v7包。


 


colorPrimary,colorPrimaryDark这两个属性是Material Design风格中规定的。具体位置如下图所示:



android状态栏颜色修改_状态栏颜色_03

 


 


 


方式三:


1.在res/values-v19文件夹下添加styles.xml文件内容如下

<           stylename           ="AppTheme"parent="@style/BaseAppTheme">          

                       <           itemname           ="android:windowTranslucentStatus">true</           item           >          

            </           style           >


2.顶部标题的控件设置两个属

android:background="@android:color/holo_blue_bright" android:fitsSystemWindows="true"

则状态栏会保持与设置fitsSystemWindow属性的控件的背景颜色一致。


 


4.4环境下


上面的方式三也适用4.4环境。不过4.4和5.x下显示的效果有差异。根据本人测试结果来看,不同的手机厂商对于这种情况下,状态栏有的是渐变,有的是添加了一层黑色半透明层。


 


存在bug及解决办法


修改windowTranslucentStatus/Navigation="true"。 会导致EditText输入时,即使使用了 adjustResize,软键盘也会挡住 EditText


解决办法参考:


https://www.zhihu.com/question/31468556/answer/52136849?utm_source=weibo&utm_medium=weibo_share&utm_content=share_answer&utm_campaign=share_button




 


其他参考资料:


http://www.bkjia.com/Androidjc/971024.html


http://blog.sina.com.cn/s/blog_6e334dc70102ve7d.html




源代码: https://github.com/leon-HM/StatusBar


 



当你都实现的时候我就会说一个快捷方式同时适配4.4和5.1的 其他没测试估计也可以


<style name="MainTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimaryDark">@color/color_bg</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


<color name="color_bg">#000000</color>


那你肯定会说 逗我玩的是吧 这么多功能 这么点文字就实现了??

我也很无奈啊  前面都是我在网上搜索的 然后最后面就自己想出来的方法........................


  对啦 这个样式一定要给BaseActivity  然后都继承BaseActivity