android状态栏颜色修改

<p>android状态栏颜色修改</p><p></p><p>状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的。</p><p></p><p>5.x环境下</p><p>方式一,状态栏将显示为纯净的颜色,没有渐变效果</p><pre class="brush:as3;toolbar:false">/** *状态栏相关工具类 * */ publicclassStatusBarUtils{ publicstaticvoidsetWindowStatusBarColor(Activityactivity,intcolorResId){ try{ if(Build.VERSION.SDK_INT&gt;=Build.VERSION_CODES.LOLLIPOP){ Windowwindow=activity.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(activity.getResources().getColor(colorResId)); //底部导航栏 //window.setNavigationBarColor(activity.getResources().getColor(colorResId)); } }catch(Exceptione){ e.printStackTrace(); } } publicstaticvoidsetWindowStatusBarColor(Dialogdialog,intcolorResId){ try{ if(Build.VERSION.SDK_INT&gt;=Build.VERSION_CODES.LOLLIPOP){ Windowwindow=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(Exceptione){ e.printStackTrace(); } } }</pre><p>  </p><p></p><p>效果图如下:状态栏被改成android.R.color.holo_blue_bright,标题栏颜色可以和状态栏一样,EditText的输入也没有受影响</p><p><img src="/up_pic/201805/290937226540.png" title="290937226540.png" alt="1.png"/></p><p>ps:如果顶部为渐变效果,可能是在主题中设置windowTranslucentStatus=true属性。</p><p></p><p>方式二:</p><p>通过Style来修改状态栏颜色。</p><p>1.设置 colorPrimary,colorPrimaryDark两个颜色。</p><pre class="brush:as3;toolbar:false">&lt;stylename=&quot;AppTheme&quot;parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt; &lt;itemname=&quot;colorPrimary&quot;&gt;@android:color/holo_blue_bright&lt;/item&gt; &lt;itemname=&quot;colorPrimaryDark&quot;&gt;@android:color/holo_blue_bright&lt;/item&gt; &lt;/style&gt;</pre><p>2. AndroidManifest.xml文件中的targetSdkVersion必须设置在21以上。</p><p>3.parent主题必须是Theme.AppCompat开头,兼容包下的主题,所以必须一用v7包。</p><p></p><p>colorPrimary,colorPrimaryDark这两个属性是Material Design风格中规定的。具体位置如下图所示:</p><p></p><p><img src="/up_pic/201805/290937291198.jpg" title="290937291198.jpg" alt="2.jpg"/></p><p></p><p>方式三:</p><p>1.在res/values-v19文件夹下添加styles.xml文件内容如下</p><pre class="brush:as3;toolbar:false">&lt;stylename=&quot;AppTheme&quot;parent=&quot;@style/BaseAppTheme&quot;&gt; &lt;itemname=&quot;android:windowTranslucentStatus&quot;&gt;true&lt;/item&gt; &lt;/style&gt;</pre><p>2.顶部标题的控件设置两个属</p><pre class="brush:as3;toolbar:false">android:background=&quot;@android:color/holo_blue_bright&quot; android:fitsSystemWindows=&quot;true&quot;</pre><p>则状态栏会保持与设置fitsSystemWindow属性的控件的背景颜色一致。</p><p></p><p>4.4环境下</p><p>上面的方式三也适用4.4环境。不过4.4和5.x下显示的效果有差异。根据本人测试结果来看,不同的手机厂商对于这种情况下,状态栏有的是渐变,有的是添加了一层黑色半透明层。</p><p></p><p>存在bug及解决办法</p><p>修改windowTranslucentStatus/Navigation=&quot;true&quot;。 会导致EditText输入时,即使使用了 adjustResize,软键盘也会挡住 EditText</p><p>解决办法参考:</p><p>https://www.zhihu.com/question/31468556/answer/52136849?utm_source=weibo&amp;utm_medium=weibo_share&amp;utm_content=share_answer&amp;utm_campaign=share_button</p><p></p><p></p><p>其他参考资料:</p><p>http://blog.csdn.net/lmj623565791/article/details/48649563</p><p>http://www.bkjia.com/Androidjc/971024.html</p><p>http://blog.sina.com.cn/s/blog_6e334dc70102ve7d.html</p><p>源代码:https://github.com/leon-HM/StatusBar</p>
RangeTime:0.006622s
RangeMem:207.57 KB
返回顶部 留言