方案一:
1.在Activity onCreate()生命周期加入以下代码(注意:在setContentView()之前添加 以下代码):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置状态栏覆盖到应用之上,不占固定位置
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.xxxx);
...
}
总结:设置上属性我有BUG,在下拉状态栏时,状态会显示黑色状态背景,壁纸无法可见,终极解决方案参考方案二。
方案二:
1.在style.xml添加自定义属性:
<style name="ActivityTransparentTheme" parent="AppTheme">
...
<!--系统顶部状态栏悬浮到应用之上,不占有固定位置-->
<item name="android:windowTranslucentStatus">true</item>
...
</style>
2.在项目AndroidManifest.xml添加发下自定页面样式,代码如下:
<application
...
android:theme="@style/ActivityTransparentTheme">
...
</application>
3.在activity对应layout布局父布局节点添加以下属性
// 解决沉浸式中状态栏与布局重合
// android:fitsSystemWindows="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_launcher"
android:fitsSystemWindows="true">
...
</RelativeLayout>
效果图如下: