问题
如图所示,标题栏和背景颜色不统一,整体视感不协调。
背景
1.Android 4.4之前状态栏默认黑色,不能改变
2.Android 4.4可以设置状态栏是否透明
3.Android 5.0+可以主动设置状态栏背景
预期效果
状态栏和toolbar或背景色一致,如:
初步方案
1.Android 4.4以下,不做处理
2.Android 4.4+ ,设置状态栏透明
实现
style
创建values、values-v19、values-v21三个目录,分别在三个目录下创建styles.xml文件,在该文件中创建相同主题,暂且取名叫TranslucentSystemBar吧。
values
<style name="TranslucentSystemBar" parent="@style/AppTheme.NoActionBar"/>
values-v19
<style name="TranslucentSystemBar" parent="@style/AppTheme.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
values-v21
<style name="TranslucentSystemBar" parent="@style/AppTheme.NoActionBar" >
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x 开始需要把状态栏颜色设为透明,否则会呈现系统默认的灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
android:fitsSystemWindows="true"
要在xml文件中添加
android:fitsSystemWindows="true"
属性,该属性的作用是布局时,忽略状态栏高度对整体布局的影响。如果属性设置为false的话,即
android:fitsSystemWindows="false"
效果如下:
参考如下:
http://www.jianshu.com/p/0acc12c29c1b
http://www.jianshu.com/p/140be70b84cd?utm_source=tuicool&utm_medium=referral