溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android應(yīng)用中啟動頁出現(xiàn)白屏如何解決

發(fā)布時間:2020-12-11 14:41:17 來源:億速云 閱讀:445 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Android應(yīng)用中啟動頁出現(xiàn)白屏如何解決,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Activity中的代碼:

/**
 * 啟動頁,顯示傾旅的logo,停頓2秒后跳轉(zhuǎn)
 */
public class LunchActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lunch);

    //開啟子線程進行停頓。如果在主線程停頓的話,會造成主頁面卡死,所以在子線程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)到主頁面
  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:id="@+id/imageView5"
    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來顯示一張圖片。并把背景設(shè)置為橘色

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

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

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

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

  • 背景是在主題中指定的,首先設(shè)置一個主題,把背景改成我們要的。一般和我們的啟動頁保持一致,這樣的話就不會看起來像兩個啟動頁一樣。也可以像網(wǎng)易云音樂那樣,背景設(shè)置成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"

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

看完上述內(nèi)容,你們對Android應(yīng)用中啟動頁出現(xiàn)白屏如何解決有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI