TV开屏页

2022-04-08  本文已影响0人  BeRicher

目前安卓的开屏已经不推荐自己创建SplashActivity了,而是使用系统自带的开屏。

安卓12以后自带这个依赖,安卓12以前我们需要引入适配依赖:

在app模块下:

implementation 'androidx.core:core-splashscreen:1.0.0-beta02'

在AndroidManifest.xml中,将MainActivity设置为主入口:

 <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
  </intent-filter>

在AndroidManifest.xml中,修改application节点,增加

    <application
        android:theme="@style/SplashTheme">

在values目录下的themes.xml中,增加

    <style name="MainTheme" parent="@style/Theme.Leanback" />
    <style name="SplashTheme" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">#CCCCCC</item>
        <item name="windowSplashScreenAnimatedIcon">@mipmap/logo</item>
        <item name="postSplashScreenTheme">@style/MainTheme</item>
    </style>

新建values-v31目录及themes.xml文件,增加:

<resources>
    <style name="SplashTheme" parent="Theme.SplashScreen">
        <item name="android:windowSplashScreenBackground">#CCCCCC</item>
        <item name="android:windowSplashScreenAnimatedIcon">@mipmap/logo</item>
    </style>
</resources>

MainActivity中,onCreate方法改为:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen()
        setContentView(R.layout.activity_main)
        preInitApp();
    }

    //进入主页面之前需要预加载的工作
    private fun preInitApp(){
        val contentView: View = findViewById(android.R.id.content)
        contentView.viewTreeObserver.addOnPreDrawListener {
            //todo 处理应用程序的初始化工作
            Thread.sleep(3000)
            false
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读