今天就跟大家聊聊有关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照片即可。怎么设置呢?
在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应用中启动页出现白屏如何解决有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。