类似于首页这种有几个fragment的页面,当只有其中一个图片需要显示在状态栏上,又要兼容全面屏下导航栏不被底部tab栏所覆盖,经过诸多尝试,得到以下方案
分别创建values、values-v19、values-v21三个目录,分别在三个目录下创建styles.xml文件,依次添加下面代码
-value-
<style name="TranslucentSystemBar" parent="Theme.AppCompat.Light.NoActionBar"/>
-values-v19-
<style name="TranslucentSystemBar" parent="Theme.AppCompat.Light.NoActionBar">
<!--状态栏是否透明-->
<item name="android:windowTranslucentStatus">true</item>
<!--导航栏是否透明-->
<item name="android:windowTranslucentNavigation">false</item>
</style>
-values-v21
<style name="TranslucentSystemBar" parent="NightTheme">
<!--状态栏是否透明-->
<item name="android:windowTranslucentStatus">true</item>
<!--是否绘制背景-->
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<!--导航栏是否透明-->
<item name="android:windowTranslucentNavigation">false</item>
<!--Android 5.x 开始需要把状态栏颜色设为透明,否则会呈现系统默认的灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
最后在布局页面的根布局添加
android:fitsSystemWindows="false"
在Manifest中添加主题样式
android:theme="@style/TranslucentSystemBar"