关于android:全屏沉浸式UI快速流程1

1次阅读

共计 2530 个字符,预计需要花费 7 分钟才能阅读完成。

1. 设置全屏

<!-- 全屏 - 启动页最下面的各种状态栏也去掉,清单中启动 Activity 援用 -->
    <style name="FullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

<!-- 没有 title,在 application 的 theme 中援用 -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!--<item name="android:windowNoTitle">true</item>-->
    </style>
 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round" 
        android:supportsRtl="true"
        android:theme="@style/AppTheme"> // 在这援用
        
<activity android:name=".StartActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/FullScreenTheme" // 在这援用
            >   

2 启动页倒计时跳转

https://gitee.com/hjmayun/codes/wofkb9z1y72e845xid0jg24

3 沉迷式状态栏

沉迷式状态栏最好不要从代码中设置,在代码中会有个变色的过程,不是想要的成果

1 新建两个文件夹 values 的 -v19,-v21 的:

v19 的 styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--values-19. v19 开始有 android:windowTranslucentStatus 这个属性,这个属性为将状态 -->
    <style name="TranslucentStatusTheme" parent="AppTheme">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

v21 的 styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TranslucentStatusTheme" parent="AppTheme">
         <item name="android:windowTranslucentStatus">false</item>
        <!-- 下部的导航栏也设置成沉迷 -->
         <item name="android:windowTranslucentNavigation">true</item>
         <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <!-- Android 5.x 开始须要把色彩设置通明,否则导航栏会出现零碎默认的浅灰色 -->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

能够看到,零碎应用的都是 TranslucentStatusTheme 这个主题,只不过在不同 api 下他的属性有差异。

在 values 的 style.xml 下

我的 AppTheme 是 NoActionBar 的

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!--title(actionbar) 的色彩 -->
        <item name="colorPrimary">@color/black</item>
        <!-- 状态栏色彩 -->
        <item name="colorPrimaryDark">@color/transparent</item>
        <!-- 浮动的 button、光标色彩 -->
        <item name="colorAccent">@color/bluelogin</item>
    </style>

写一个 TranslucentStatusTheme,空实现就能够:

<style name="TranslucentStatusTheme" parent="AppTheme">
    </style>

2 在清单 manifest.xml 中,application 援用这个主题即可

 <application
        .....
        android:theme="@style/TranslucentStatusTheme">

3 留神

在 layout 的 xml 下的根布局须要增加属性:android:fitsSystemWindows=”true”

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    ....
    android:fitsSystemWindows="true"
   ....    >

增加这个属性之后,底下的导航栏才会有一个绝对的沉迷式的展现。

以上的沉迷成果最好别设置 title,否则会有各种坑,计算起来很麻烦

4 参考

Android 沉迷式状态栏适配计划

android 沉迷式状态栏、fitsSystemWindows、标题栏折叠

正文完
 0