Android 8.0(Oreo) 启动页面

1. 为什么需要启动画面?

Android应用程序首次启动应用程序时需要一些时间来加载(冷启动)。这是应用程序需要自行配置的时间。在此延迟期间,布局不会加载,因此会弹出一个空白屏幕,直到应用程序完全加载,然后继续其正常行为。所以为了提升用户的体验,需要显示一个启动画面,我们不会使用布局文件,而是将启动画面的背景指定为Activity的主题背景。

2. 怎么使用?

2.1 创建一个SplashscreenActivity继承AppCompatActivity 的Activity.

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

public class SplashscreenActivity extends AppCompatActivity {

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)
      
    }
}

2.2 在drawable目录下新建 splashscreen.xml 文件.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:gravity="center|fill"
            android:src="@drawable/your_splash_screen" />
    </item>
</layer-list>

2.3  在Android API 26之前,我们将上面的drawable设置为values / styles.xml文件中的windowbackground

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

2.4 当Android API​​ 26及以上版本,我们现在可以将drawable设置为新提供的windowSplashscreenContent属性,该属性引用drawable以用作窗口的初始屏幕内容。此drawable现在将放置在windowBackground属性的顶部,其边界由系统栏插入。创建一个values-v26 / styles.xml .

<style name="Splashscreen" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowSplashscreenContent">@drawable/splashscreen</item>
</style>

2.5 最后在AndroidManifest.xml中将上述主题设置为Activity的主题.

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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值