完美兼容4.4及以上系统实现沉浸式状态栏

xiaoxiao2021-02-28  86

先上图看效果

这是Nexus 4.4上的展示效果

这是Nexus 5.0上的展示效果

 

Google允许开发者在Android 4.4以上设置状态栏透明或半透明,布局填充全屏;而在5.0以上可直接设置状态栏颜色。

所以为了兼容4.45.0之间系统,整理步骤如下:

 

解决思路是:将状态栏设置为透明,标题栏填充全屏,判断4.4以上标题栏高度为原设计高度+状态栏高度,标题栏设置内距上边距为状态栏高度;如果是4.4以下则使用原设计高度。

一、修改styles文件

values中的styles

<style name="TranslucentTheme" parent="AppTheme"> </style>

values-v19styles

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> </style>在values-v21的styles中

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> <item name="android:statusBarColor">@android:color/transparent</item> </style>

二、AndroidManiFest文件中设置对应ActivityTheme

    如:

<activity android:name=".activity.StatusBarActivity" android:theme="@style/TranslucentTheme"/>

三、修改dimens文件

vaulesdimens文件中

<dimen name="title_translucent_height">50dp</dimen> <dimen name="title_margin_top">0dp</dimen>在values-v19的dimens文件中

<dimen name="title_translucent_height">75dp</dimen> <dimen name="title_margin_top">25dp</dimen>

四、在布局文件中

TitleBar设置 如:

<TextView android:layout_width="match_parent" android:layout_height="@dimen/title_translucent_height" android:paddingTop="@dimen/title_margin_top" android:background="#450125" android:fitsSystemWindows="true" />

高度和内距上设置刚刚在dimens中的定义的数据

五、特别注意,将TitleBar设置

android:fitsSystemWindows="true"

这样的ok了!

转载请注明原文地址: https://www.6miu.com/read-66915.html

最新回复(0)