您是否曾在许多应用程序中看到过在启动第一个 Activity 之前出现的页面,并且不知何故它就像一个等待页面?!我是 Android 初学者,当我单击我的应用程序时,首先会出现一个空白页面,然后 3 秒后出现主要 Activity 。但在许多应用程序中,第一个 Activity 之前有一种自定义页面,例如进度条或其他东西。在启动我的应用程序之前,如何自定义那个不太漂亮的空白页面?!提前致谢。
最佳答案
从技术上讲,您所说的东西称为启动屏幕。
启动画面用于显示冷启动,我们可以在其中准备好运行应用程序。 Google 提倡使用它。
显示闪屏的方法很少。
您可以使用
CountDownTimer
像这样。在SplashActivity.java
private int TIME_OUT = 3000;<p></p> <pre><code>CountDownTimer countDownTimer = new CountDownTimer(TIME_OUT, TIME_OUT) { @Override public void onTick(long millisUntilFinished) { // Leave this } @Override public void onFinish() { // Start your app main activity Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); // close this activity finish(); } </code></pre>
}.start();
You can use
Handler
like this. InSplashActivity.java
private static int TIME_OUT = 3000; new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); finish(); }
},TIME_OUT);
使用主题 - 正确的做法
添加background_splash.xml
可绘制文件夹中的文件
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
在 styles.xml
中添加这些行
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
在 AndroidManifest.xml
中添加这些行
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
最后添加一个名为 SplashActivity.java
的 Activity 并添加这些代码
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
关于java - 在 Android 应用程序中显示启动屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139777/