普通屏幕、刘海屏的沉浸式状态栏适配
一、沉浸式状态栏(状态栏透明,布局内容延伸进状态栏,Android4.4,API19以上)
第一种方式: 为Activity设置style
1、清单文件中:
<activity
android:name=".SecondActivity"
android:theme="@style/ImmersiveScreenTheme" />
2、style文件中:
<style
name="ImmersiveScreenTheme" parent="AppTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
第二种方式: 在Activity的onCreate()中为Window添加Flag
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
总结: 这两种方式都有个问题,如果布局中有控件,控件也会延伸到状态栏内部。
解决办法(三种方法):
1、布局文件中,为firstSystemWindows跟视图设置属性
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_1"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context=".ThirdActivity">
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:text="按钮" />