关于android:Android-SplashPage实现应用秒开3步

3次阅读

共计 1435 个字符,预计需要花费 4 分钟才能阅读完成。

Android 利用冷启动时,须要从 Application 开始启动,加载工夫就会比拟长,这段时间里,用户所能看到的就是”白屏“(这是因为默认的 AppTheme 的 android:windowBackground 默认是设置成红色的),因而我认为真正的启动页就应该是让用户点开利用时看到的不是”白屏“,而是咱们创立的一个页面,能够是一张图片、一段文字。
就会让人感觉到,这个利用能够秒开。

1. 首先在 drawable 目录下新建一个 splash\_screen.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/colorPrimary"/>
    <item>
        <bitmap android:src="@drawable/ic_logo"
            android:gravity="center"/>
    </item>
</layer-list>

咱们应用 layer-list 标签创立一个图层列表,理论就是一个 LayerDrawable,设置一个背景,而后放上利用图标,这是我想展现的启动页,能够依据本人的须要自行定义。

2. 而后在 style.xml 文件中定义一个 SplashTheme

<resources>
    ...
    
    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/splash_screen</item>
    </style>

</resources>

这里只须要将窗口背景设置为咱们方才定义的 LayerDrawable。

3. 而后须要在 AndroidMenifest.xml 文件中将咱们的主页面,我这里是 MainActivity 的 android:theme 设置成咱们定义的 SplashTheme

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

    ...

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        ...
    </application>

</manifest>

是不是很简略这样就能够了

Android 零根底系列教程:【Android 根底课程】

本文转自 https://juejin.cn/post/6963874611960217631,如有侵权,请分割删除。

正文完
 0