java - 在 Android 应用程序中显示启动屏幕

标签 java android user-interface apk splash-screen

您是否曾在许多应用程序中看到过在启动第一个 Activity 之前出现的页面,并且不知何故它就像一个等待页面?!我是 Android 初学者,当我单击我的应用程序时,首先会出现一个空白页面,然后 3 秒后出现主要 Activity 。但在许多应用程序中,第一个 Activity 之前有一种自定义页面,例如进度条或其他东西。在启动我的应用程序之前,如何自定义那个不太漂亮的空白页面?!提前致谢。

最佳答案

从技术上讲,您所说的东西称为启动屏幕

启动画面用于显示冷启动,我们可以在其中准备好运行应用程序。 Google 提倡使用它。

显示闪屏的方法很少。

  1. 您可以使用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();

  2. You can use Handler like this. In SplashActivity.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);

  3. 使用主题 - 正确的做法

添加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/

相关文章:

python - 一个Python脚本,可以自动在网站中输入一些文本并获取其源代码

java - 使用 DisplayTag 时如何自定义 paging.banner.some_items_found

android - 寻呼机指示器 flutter

java - 无法为接口(interface) android.app.IAlarmManager 调用无参数构造函数

android - 相机捕获的 ImageView 大小

android - 如何在 android 上更新 RecyclerView 中的数据

java - 如何在没有初始化的情况下将图像转换为 BufferedImage?

c++ - QT4:带圆角的透明窗口

java - XStream Java 对象到 XML 跳过一些字段

java - 如何向 GXT 网格添加行