单个 Activity 应用中的启动画面方法

Splash-screen approach in single Activity app

我正在尝试确定创建应用启动画面的最佳方法,同时考虑了 Google 关于尽可能选择单个 Activity 应用的最新建议。

看这里:

"The new approach is to use one-activity structure whenever possible."

这里:

"Today we are introducing the Navigation component as a framework for structuring your in-app UI, with a focus on making a single-Activity app the preferred architecture."

我发现的任何好的启动画面方法都有专门的 Activity 启动画面:

See here

and here

有没有其他人有过在单个 Activity 应用程序中创建启动画面的经验?单个 Activity 建议是否包括闪屏或它是一种特殊情况?有人对此有什么好的例子或建议吗?

干杯, 保罗.

如果您在布局中使用 ConstraintLayout,则可以使用 Android 的 Group class 对多个视图进行分组。详情请参考以下link

https://developer.android.com/reference/android/support/constraint/Group

此 class 控制一组引用小部件的可见性。通过添加到以逗号分隔的 ID 列表来引用小部件,例如:

 <android.support.constraint.Group
          android:id="@+id/group"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:visibility="visible"
          app:constraint_referenced_ids="button4,button9" />

仅供参考 - 多个组可以引用相同的小部件 - 在这种情况下,XML 声明顺序将定义最终的可见性状态(最后声明的组将有最后一个词)。

希望这能帮助您解决问题。

我使用的方法如下:

首先为背景定义一个drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/green"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

2。定义要在启动画面中使用的新样式:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

3。让您的 activity 工具使用启动主题:

<activity
    android:name=".MainActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

4。在创建时,在超级调用之前和设置内容视图之前设置默认应用程序主题:

override fun onCreate(savedInstanceState: Bundle) {
    setTheme(android.R.style.AppTheme)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main);
}

这种方法是我在多个活动中一直使用的方法,因为它遵循 google 制定的准则:它会立即显示启动画面并且不会停留超过需要的时间。