【发布时间】:2021-06-20 15:43:32
【问题描述】:
在网上查找时,我发现在 Android 应用上实现启动画面的方法不止一种。有人使用“splash.xml”布局和“splash_theme”(通常是全屏)创建了一个新的“splashActivity”,其他一些人使用没有“splash.xml”布局的“splashActivity”,而只是一个带有“splashscreenDrawable”的主题。 xml”作为背景。我正在使用最后一个,因为与第一种情况相比,它似乎更快(也许主题+drawable 不像布局那么重?),但哪个更好?在哪些情况下我应该使用第一个或第二个?
该应用使用android oreo 8.0,我正在使用kotlin
这是代码(以防问题表达不好)
MainActivity.kt(什么都不做)
package com.example.mobileprogrammingproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
activity_main.xml(只是一个带有电视和 btn 的测试布局)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0"
android:layout_marginStart="10dp"
android:textSize="26sp">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press me"
app:layout_constraintTop_toBottomOf="@+id/titleTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
SplashScreen.kt(调用 MainActivity.kt 的启动器活动)
package com.example.mobileprogrammingproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
class SplashScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(Intent(applicationContext, MainActivity::class.java))
finish()
}
}
AndroidManifest.xml(我已将 laucher 活动从 MainActivity.kt 更改为 SplashScreen.kt)
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobileprogrammingproject">
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:roundIcon="@drawable/icon"
android:supportsRtl="true"
android:theme="@style/Theme.MobileProgrammingProject">
<activity
android:name=".SplashScreen" android:theme="@style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
和启动主题(使用可绘制对象)
<style
name="SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen_drawable</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
这是 splash_screen_drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/splash_background_color"
android:drawable="@android:color/black" />
<item>
<bitmap
android:src="@drawable/dicce_logo"
android:gravity="center"/>
</item>
</layer-list>
【问题讨论】:
-
好吧,你回答了你自己的问题。最好的实现是使用主题而不是新活动。 SplashScreen 用于在应用程序加载时显示某些内容。为此目的使用另一个活动只是扩展应用加载状态,这是不想要的
标签: android kotlin layout splash-screen drawable