温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中启动页出现白屏如何解决

发布时间:2020-12-11 14:41:17 来源:亿速云 阅读:429 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关Android应用中启动页出现白屏如何解决,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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: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来显示一张图片。并把背景设置为橘色

最后再把启动页活动设置为主活动:

<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,但是启动页是放广告,但是这会影响用户体验(为了收入打点广告也是可以理解的)。看代码:

在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应用中启动页出现白屏如何解决有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI