我是安卓的新手。我想為我的應用程式添加一個閃屏。我瀏覽了這個官方檔案。
。它說
當用戶在應用程式的行程未運行(冷啟動)或活動未創建(熱啟動)時啟動應用程式時,會發生以下事件。(1)系統使用你定義的主題和任何影片來顯示閃屏。2) 當應用程式準備就緒時,閃屏被取消,應用程式被顯示。盡管像下面給出的代碼片段那樣將一個活動作為閃屏來實作,但它可以完成這項作業
class SplashScreenActivity : AppCompatActivity() {
override fun onCreate(sedInstanceState: Bundle? ){
super.onCreate(s savedInstanceState)
setContentView(R.layout.activity_splash_screen)
supportActionBar?.hide()
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
完成()
}, 2000)
}
}
但我猜這并不是傳統的方式,因為閃屏成為了應用程式行程的一部分,它在長達幾毫秒的黑屏之后開始。
我猜測這時系統正在獲取應用程式的資料資源,這時應該啟動閃屏,至少Google play store或Whatsapp或其他所有已安裝的應用程式都是如此,閃屏啟動是即時的。
現在,正如檔案所堅持的那樣,當我試圖設定主題屬性時,我得到了錯誤的提示無法解決符號'android:windowSplashScreenBackground'。這是我的style.xml
<?xml version="1.0" encoding="utf-8"? >
<resources xmlns:tools="http://schemas.android.com/tools"/span>
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="主題。 Books" parent="Theme.MaterialComponents.DayNight.DarkActionBar">/span>
<item name="colorPrimary"/span>> @color/purple_500</item>。
<item name="colorPrimaryVariant"/span>> @color/purple_700</item>。
<item name="colorOnPrimary"/span>> @color/white</item>。
<item name="colorSecondary"/span>> @color/teal_200</item>。
<item name="colorSecondaryVariant"/span>> @color/teal_700</item>。
<item name="colorOnSecondary"/span>> @color/black</item>。
<item name="android: statusBarColor" tools:targetApi="l">/span>? attr/colorPrimaryVariant</item>?
<!-- Unresolved -->
<item name="android: windowSplashScreenBackground">@color/black</item>
<item name="android: windowSplashScreenAnimationDuration">1000</item>
<item name="android: windowSplashScreenAnimatedIcon">@drawable/books_logo</item>/span>
</style>>
</resources>
因此。 1.如何克服這個問題?
2.
2.有其他的方法嗎? 3.
3.我錯過了什么嗎?是否有一些方法可以將活動從應用程式行程中分離出來?
感謝您的任何幫助。謝謝! uj5u.com熱心網友回復: 你有沒有嘗試過按照這篇文章來做? Implementing Core Splashscreen API/a> 我從教程中得到的資訊是: 你需要修改style.xml,如文章中所示,使用自定義的SplashScreen屬性(然后將App Theme設定為該屬性) 看這里 只需移除你的SplashScreenActivity,使用Handler.postDelayed也不是處理這個問題的好方法(Library會自己處理切換顯示的內容,只需使用你的MainActivity)
標籤:setContentView(R.id.***)之前呼叫installSplashScreen()。
