成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android 啟動(dòng)頁(yè)白屏解決方案

瀏覽:60日期:2022-09-21 14:03:38

當(dāng)我們打開(kāi)app的時(shí)候是不是會(huì)有一瞬間的白屏然后再進(jìn)入主活動(dòng),雖然這并不會(huì)造成什么不好的后果,但是感覺(jué)用戶體驗(yàn)就不是很好。像網(wǎng)易云音樂(lè)等等,打開(kāi)一瞬間就顯示了他們的loge,無(wú)縫銜接,沒(méi)有白屏,怎么做到的呢?

一開(kāi)始我的思路是這樣的。可能是因?yàn)槲覀兊闹骰顒?dòng)邏輯太多,所以加載會(huì)變慢,導(dǎo)致顯示白屏。如果使用一個(gè)只顯示一張本地圖片的活動(dòng),那會(huì)不會(huì)就不會(huì)顯示白屏了呢。話不多說(shuō)我們嘗試一下:

Activity中的代碼:

/** * 啟動(dòng)頁(yè),顯示傾旅的logo,停頓2秒后跳轉(zhuǎn) */public class LunchActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lunch); //開(kāi)啟子線程進(jìn)行停頓。如果在主線程停頓的話,會(huì)造成主頁(yè)面卡死,所以在子線程sleep兩秒后跳轉(zhuǎn) new Thread(new Runnable() { @Override public void run() {try { Thread.sleep(1000);} catch (InterruptedException e) { e.printStackTrace();}start();LunchActivity.this.finish(); } }).start(); } //跳轉(zhuǎn)到主頁(yè)面 private void start(){ Intent intent = new Intent(LunchActivity.this,MainActivity.class); startActivity(intent); }}layout中的代碼:

<?xml version='1.0' encoding='utf-8'?><android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:background='#e74b37' tools:context='.LunchActivity'> <ImageView android: android:layout_width='80dp' android:layout_height='80dp' app:layout_constraintBottom_toBottomOf='parent' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintStart_toStartOf='parent' app:layout_constraintTop_toTopOf='parent' app:layout_constraintVertical_bias='0.31' app:srcCompat='@drawable/icon' /></android.support.constraint.ConstraintLayout>

這里簡(jiǎn)單指定一個(gè)imageView來(lái)顯示一張圖片。并把背景設(shè)置為橘色

最后再把啟動(dòng)頁(yè)活動(dòng)設(shè)置為主活動(dòng):

<activity android:name='com.example.qinglv.LunchActivity'> <intent-filter><action android:name='android.intent.action.MAIN' /><category android:name='android.intent.category.LAUNCHER' /> </intent-filter> </activity>

一切想的很好,完成后打開(kāi)一看,還是會(huì)白屏,怎么回事?

活動(dòng)的加載都是需要時(shí)間的,比較簡(jiǎn)單的活動(dòng)時(shí)間會(huì)少點(diǎn),但是以然會(huì)有一瞬間的白屏。那這個(gè)白屏到底是什么?就是每個(gè)活動(dòng)的背景。當(dāng)打開(kāi)一個(gè)活動(dòng)的時(shí)候,因?yàn)檫€沒(méi)加載出內(nèi)容,所以顯示的就只是背景,所以我們只需要,改變這個(gè)背景,設(shè)置為我們需要的一個(gè)logo照片即可。怎么設(shè)置呢?

背景是在主題中指定的,首先設(shè)置一個(gè)主題,把背景改成我們要的。一般和我們的啟動(dòng)頁(yè)保持一致,這樣的話就不會(huì)看起來(lái)像兩個(gè)啟動(dòng)頁(yè)一樣。也可以像網(wǎng)易云音樂(lè)那樣,背景設(shè)置成logo,但是啟動(dòng)頁(yè)是放廣告,但是這會(huì)影響用戶體驗(yàn)(為了收入打點(diǎn)廣告也是可以理解的)。看代碼:

在res-value-styles:

<style name='NewAppTheme' parent='Theme.AppCompat.Light.NoActionBar'> <!-- Customize your theme here. --> <item name='colorPrimary'>@color/colorPrimary</item> <item name='colorPrimaryDark'>@color/colorPrimaryDark</item> <item name='android:windowBackground'>@color/colorPrimary</item> <item name='colorAccent'>@color/colorAccent</item> </style>

重點(diǎn)是這句<item name='android:windowBackground'>@color/colorPrimary</item>這里我指定的是一種顏色你們也可以指定一張圖片

再給啟動(dòng)頁(yè)活動(dòng)指定主題:

在:AndroidManifest:

<activity android:name='com.example.qinglv.LunchActivity' android:theme='@style/NewAppTheme'> <intent-filter> <action android:name='android.intent.action.MAIN' /> <category android:name='android.intent.category.LAUNCHER' /> </intent-filter> </activity>

重點(diǎn)是這句android:theme='@style/NewAppTheme'

然后再打開(kāi)的時(shí)候,就會(huì)發(fā)現(xiàn)不會(huì)了。原本顯示的白屏變成了我們?cè)O(shè)置好的圖片。

以上就是Android 啟動(dòng)頁(yè)白屏解決方案的詳細(xì)內(nèi)容,更多關(guān)于Android 啟動(dòng)頁(yè)白屏的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章: