Android通过NoActionBar设置沉浸式

新建一个style,继承NoActionBar;

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--&lt;!&ndash; 设置全屏&ndash;&gt;-->
        <!--<item name="android:windowFullscreen">true</item>-->
        <!-- 设置无标题-->
        <item name="android:windowNoTitle">true</item>
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
    </style>

在mainfest.xml文件下的application设置android:theme="@style/AppTheme";

继承AppTheme来根据需求来设置一个自己需要的样式;

<style name="AppTheme.NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="alertDialogTheme">@style/MyAlertDialogStyle</item>
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
    </style>

在自己的activity设置style,android:theme="@style/AppTheme.NoActionBar";

这样子我们需要的效果就行了;

设置图片背景状态栏,我知道的的方法有:

  • 通过样式<item name="android:windowFullscreen">true</item>设置全屏;
  • 沉浸式状态栏设置透明

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明导航栏
        }

很多人都将getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);这个方法设置进去,其实这个是不行的,因为在7.0以后出现的全屏手机适配的时候,会将导航栏给透明化,遮挡住了我们的布局。所以我不推荐使用这个。

猜你喜欢

转载自blog.csdn.net/qq_33240767/article/details/81198187