普通屏幕、刘海屏的沉浸式状态栏适配
一、沉浸式状态栏(状态栏透明,布局内容延伸进状态栏,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="按钮" />
</LinearLayout>
2、根据状态栏高度手动设置paddingTop
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle save

本文详细介绍了Android中实现沉浸式状态栏的多种方法,包括通过设置Activity style、在onCreate()中添加Flag,以及解决控件延伸到状态栏内部的问题。同时,讨论了全屏风格的实现,并提到了如何处理底部导航栏的透明效果。对于状态栏颜色的修改,特别是文字和图标颜色的适配,文中提供了针对不同系统版本的解决方案,并推荐了第三方库ImmersionBar和UltimateBarX作为辅助工具。
最低0.47元/天 解锁文章
1万+





