平常在写一个新项目的时候,总是因为设置沉浸式效果而搞了很久,因为网上的很多说法都并不是完全正确,最后不了了之,放任不管去做其他功能了。现在自己记录下来一套正确的设置方法,方便之后的查阅。
(这套设置方法不适合与Toolbar一起使用,如果Activity中使用到了Toolbar,还是另寻他法吧。。。)
第一步:在Activity的onCreate方法中加入下面这行代码
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
复制代码这行代码要在super.onCreate(savedInstanceState);之前调用,不然会报错
第二步:自定义一个沉浸式主题
<style name="ImmersiveTheme" parent="(application所使用的全局主题名称)">
    <item name="windowNoTitle">true</item>
    <!-- 下面两条属性是Android 4.4版本以上才能设置 -->
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <!-- 下面这条属性是Android 5.0版本以上才能设置 -->
    <!-- 从Android 5.0开始需要把颜色设置为透明,否则导航栏会呈现系统默认的浅灰色 -->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
复制代码第三步:在Activity的xml文件中设置android:fitsSystemWindows=”true”
<LinearLayout
    ......
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ......
        android:fitsSystemWindows="true">
        ......
    </LinearLayout>
    <TextView
        ......
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        ......
    </TextView>
</LinearLayout>
复制代码在所有可以接触到状态栏的二级标签中设置属性android:fitsSystemWindows="true",注意不要在根标签中设置该属性
第四步:在AndroidManifest.xml中设置沉浸式主题
<!-- 全局设置 -->
<application
        ......
        android:theme="@style/ImmersiveTheme">
        ......
</application>
<!-- Activity设置 -->
<activity
    ......
    android:theme="@style/ImmersiveTheme">
    ......
</activity>
复制代码© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
    


















![[02/27][官改] Simplicity@MIX2 ROM更新-一一网](https://www.proyy.com/wp-content/uploads/2020/02/3168457341.jpg)



![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)

![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)
