简体   繁体   English

单个Activity应用程序中的Splash-screen方法

[英]Splash-screen approach in single Activity app

I am trying to decide on the best approach for creating an app splash-screen while taking into consideration Google's latest recommendations on choosing a single Activity app whenever possible. 我正在尝试确定创建应用程序启动屏幕的最佳方法,同时考虑到Google最新的建议,尽可能选择单个活动应用程序。

See here: 看这里:

"The new approach is to use one-activity structure whenever possible." “新方法是尽可能使用单活动结构。”

and here: 和这里:

"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." “今天我们将导航组件作为构建您的应用内UI的框架,重点是将单活动应用程序作为首选架构。”

Any good splash-screen approaches I have found have a dedicated Activity for the splash screen: 我找到的任何好的启动画面方法都有一个专门用于启动画面的活动:

See here 看这里

and here 和这里

Has anyone else had any experience creating a splash screen in a single Activity app? 有没有其他人有过在单个Activity应用程序中创建启动画面的经验? Does the the single Activity recommendation include the splash-screen or is it a special case? 单个活动建议是否包含启动画面,还是特殊情况? Does anyone have any good examples or advice on this? 有没有人对此有任何好的例子或建议?

Cheers, Paul. 干杯,保罗。

The approach I use is the following: 我使用的方法如下:

First define a drawable for the background: 首先为背景定义一个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. Define a new style to use in the splashScreen: 2.定义要在splashScreen中使用的新样式:

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

3. Make your activity implement use the splash theme: 3.使您的活动实现使用splash主题:

<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. In on create, before the super invocation and before the set content view set the default app theme: 4.在创建时,在超级调用之前和设置内容视图之前设置默认应用主题:

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

This approach is the one I've been using even with multiple Activities, since it follows the guidelines laid down by google: itshows the splash right away and doesn't stay longer than needed. 这种方法是我一直在使用的方法,即使是多项活动,因为它遵循谷歌制定的指导方针:它立刻引起轰动,并且不会停留超过需要的时间。

If you are using ConstraintLayout in your layouts, you can use the Group class of Android to group multiple views. 如果在布局中使用ConstraintLayout ,则可以使用Android的Group类对多个视图进行分组。 Please refer to the following link for more information. 有关更多信息,请参阅以下链接。

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

This class controls the visibility of a set of referenced widgets. 此类控制一组引用的窗口小部件的可见性。 Widgets are referenced by being added to a comma separated list of ids, eg: 通过添加到以逗号分隔的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" />

FYI - Multiple groups can reference the same widgets -- in that case, the XML declaration order will define the final visibility state (the group declared last will have the last word). 仅供参考 - 多个组可以引用相同的小部件 - 在这种情况下,XML声明顺序将定义最终的可见性状态(最后声明的组将具有最后一个单词)。

Hope this helps you resolve the issue. 希望这可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM