每次搞沉浸式全屏的时候,都是查来查去的。这里面代码虽然简单,但内容不少。今天在此记录一下。
style/themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. --> //主题切换成NoActionBar,隐藏标题栏
<style name="Theme.InstallTool" parent="Theme.AppCompat.Light.NoActionBar">
//...
//...
<!-- Status bar color. 状态栏颜色,通常设为透明色-->
<item name="android:statusBarColor">@color/color_confirm_light</item>
<!-- Android 6.0以上 状态栏字色和图标为浅黑色-->
<item name="android:windowLightStatusBar">true</item>
<!--使背景延伸至状态栏-->
//注意:windowTranslucentNavigation 应该设置成false,否则在底部有虚拟导航按键时,会出现虚拟导航按键和底部内容重合。
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:fitsSystemWindows">true</item>
</style>
</resources>
在Mianfest中使用该主题即可。
关于底部导航栏和内容重合问题,还看到一个说法,我没实践,但我觉得说的挺对的:
不需要判断底部的导航键,android:windowTranslucentNavigation 这个属性额外设置了SYSTEM_UI_FLAG_LAYOUT_STABLE 和 SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN两个flag。所以设置windowTranslucentNavigation 为false时,顶部状态栏也会失效。我们只需要重新在代码中调用getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN),这样顶部状态栏透明并且底部的导航栏也不会覆盖布局了。
参考:Android中windowTranslucentStatus与windowTranslucentNavigation的一些设置