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

使用AndroidX Core SplashScreen库闪屏无法全屏及延长时长问题

Android SplashScreen 全屏显示及延长时长解决方案

一、让闪屏图片铺满全屏

默认使用windowSplashScreenAnimatedIcon设置的图片会被系统限制为图标尺寸,无法铺满全屏。要实现全屏背景,需改用windowSplashScreenBackground属性,并适配状态栏/导航栏透明:

修改themes.xml中的Splash主题:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.News" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">#FFFFFF</item>
        <item name="colorPrimaryVariant">#EF9A9A</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <item name="android:textColorPrimary">#ffffff</item>
    </style>

    <style name="Splash" parent="Theme.SplashScreen">
        <!-- 设置全屏背景图片 -->
        <item name="windowSplashScreenBackground">@drawable/splash</item>
        <!-- 可选:若不需要中心图标,设置为透明 -->
        <!-- <item name="windowSplashScreenAnimatedIcon">@android:color/transparent</item> -->
        <item name="postSplashScreenTheme">@style/Theme.News</item>
        <!-- 适配状态栏/导航栏透明,确保图片无遮挡 -->
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
</resources>
> 若你的`splash`图片已包含状态栏区域,以上设置即可让图片完全铺满屏幕。

## 二、延长闪屏显示时长
通过`SplashScreen.setKeepOnScreenCondition()`方法设置保持显示的条件,配合协程延迟关闭:

修改`MainActivity`代码:
```kotlin
package com.example.news

import android.content.res.ColorStateList
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class MainActivity : AppCompatActivity() {
    lateinit var tabLayout: TabLayout
    lateinit var viewPager: ViewPager2
    private val tabList = listOf("Top", "Politics", "Technology")

    private val topNewsFragment = NewsFragment.newInstance("top")
    private val politicsNewsFragment = NewsFragment.newInstance("politics")
    private val technologyNewsFragment = NewsFragment.newInstance("technology")
    private val fragmentList = listOf(topNewsFragment,politicsNewsFragment,technologyNewsFragment)
    
    private var keepSplashVisible = true

    override fun onCreate(savedInstanceState: Bundle?) {
        val splashScreen = installSplashScreen()
        
        // 设置闪屏保持显示的条件
        splashScreen.setKeepOnScreenCondition { keepSplashVisible }

        // 协程延迟关闭闪屏(示例延迟2秒)
        runBlocking {
            launch {
                delay(2000)
                keepSplashVisible = false
            }
        }

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        tabLayout = findViewById(R.id.tabLayout)
        tabLayout.setBackgroundColor(ContextCompat.getColor(applicationContext,R.color.red))
        viewPager = findViewById(R.id.viewPager)

        viewPager.adapter = MyPagerAdapter(supportFragmentManager,lifecycle,fragmentList)

        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            tab.text = tabList[position]
        }.attach()
    }
}
> 避免使用`Thread.sleep()`阻塞主线程,协程是更友好的延迟方式,可根据需求调整`delay()`的毫秒值。

内容的提问来源于stack exchange,提问作者Karan Kumar
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 13:18:10