您的位置:首頁技術文章
文章詳情頁

Android 啟動頁白屏解決方案

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

當我們打開app的時候是不是會有一瞬間的白屏然后再進入主活動,雖然這并不會造成什么不好的后果,但是感覺用戶體驗就不是很好。像網易云音樂等等,打開一瞬間就顯示了他們的loge,無縫銜接,沒有白屏,怎么做到的呢?

一開始我的思路是這樣的??赡苁且驗槲覀兊闹骰顒舆壿嬏啵约虞d會變慢,導致顯示白屏。如果使用一個只顯示一張本地圖片的活動,那會不會就不會顯示白屏了呢。話不多說我們嘗試一下:

Activity中的代碼:

/** * 啟動頁,顯示傾旅的logo,停頓2秒后跳轉 */public class LunchActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lunch); //開啟子線程進行停頓。如果在主線程停頓的話,會造成主頁面卡死,所以在子線程sleep兩秒后跳轉 new Thread(new Runnable() { @Override public void run() {try { Thread.sleep(1000);} catch (InterruptedException e) { e.printStackTrace();}start();LunchActivity.this.finish(); } }).start(); } //跳轉到主頁面 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>

這里簡單指定一個imageView來顯示一張圖片。并把背景設置為橘色

最后再把啟動頁活動設置為主活動:

<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>

一切想的很好,完成后打開一看,還是會白屏,怎么回事?

活動的加載都是需要時間的,比較簡單的活動時間會少點,但是以然會有一瞬間的白屏。那這個白屏到底是什么?就是每個活動的背景。當打開一個活動的時候,因為還沒加載出內容,所以顯示的就只是背景,所以我們只需要,改變這個背景,設置為我們需要的一個logo照片即可。怎么設置呢?

背景是在主題中指定的,首先設置一個主題,把背景改成我們要的。一般和我們的啟動頁保持一致,這樣的話就不會看起來像兩個啟動頁一樣。也可以像網易云音樂那樣,背景設置成logo,但是啟動頁是放廣告,但是這會影響用戶體驗(為了收入打點廣告也是可以理解的)??创a:

在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>

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

再給啟動頁活動指定主題:

在: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>

重點是這句android:theme='@style/NewAppTheme'

然后再打開的時候,就會發現不會了。原本顯示的白屏變成了我們設置好的圖片。

以上就是Android 啟動頁白屏解決方案的詳細內容,更多關于Android 啟動頁白屏的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
国产综合久久一区二区三区