You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jetpack Compose中实现启动页后隐藏ActionBar?

Android 12启动页结束后出现ActionBar的问题

我参考《Splash screen for below all API levels with icon without any activity》一文实现了兼容Android 12及以下版本的启动页,启动页在两个版本中都能正常显示,但Android 12设备上启动页结束后会出现ActionBar,尽管已经调用enableEdgeToEdge(),而Android 5.1设备显示正常。

设备显示效果

  • Android 12设备:
    Android12启动页后显示ActionBar
  • Android 5.1设备:
    Android5.1正常显示

相关代码

theme.xml (通用版本)

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.SpotifyApp" parent="android:Theme.Material.Light.NoActionBar" >
        <item name="android:statusBarColor">@color/black</item>
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

</resources>

theme.xml (API 31+)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Splash" parent="android:Theme.Material.Light.NoActionBar" />
    <style name="Theme.SpotifyApp" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/black</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/spotify</item>
        <item name="windowSplashScreenAnimationDuration">3000</item>
        <item name="postSplashScreenTheme">@style/Theme.Splash</item>
    </style>
</resources>

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@drawable/spotify_icon_small"
        android:label="@string/app_name"
        android:roundIcon="@drawable/spotify_icon_small"
        android:supportsRtl="true"
        android:theme="@style/Theme.SpotifyApp"
        tools:targetApi="31">
        <activity
            android:name="com.example.spotify.activities.MainActivity"
            android:exported="true"
            android:theme="@style/Theme.SpotifyApp">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Theme.kt

@Composable
fun SpotifyTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = false,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

MainActivity.kt

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        installSplashScreen()

        setContent {
            SpotifyTheme {
                CompositionLocalProvider(LocalRippleConfiguration provides null) {
                    Surface(modifier = Modifier
                        .fillMaxSize()
                        .background(MaterialTheme.colorScheme.background)){
                        MyAppNavigation()
                    }
                }
            }
        }
    }
}

解决方案

问题出在API 31+的主题配置与Activity主题的衔接上,按以下步骤调整即可解决:

  1. 完善API 31+的Theme.Splash主题
    补充隐藏ActionBar的关键属性,确保Android 12+系统完全隐藏ActionBar:

    <style name="Theme.Splash" parent="android:Theme.Material.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
    
  2. 移除MainActivity的重复主题配置
    启动屏结束后会自动切换到postSplashScreenTheme指定的Theme.Splash,无需在MainActivity中重复设置启动页主题:

    <activity
        android:name="com.example.spotify.activities.MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
  3. 调整enableEdgeToEdge()调用时机
    先安装启动屏,再配置边缘到边缘的系统UI,确保启动屏切换后生效:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen()
        enableEdgeToEdge()
        setContent {
            // 原有内容
        }
    }
    

内容的提问来源于stack exchange,提问作者Fatima Jamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 17:14:50