重构一:欢迎页

本欢迎页只有一张图片,默认展示3秒,采用theme方式进行设置。

第一步: 创建一个类型为Empty Activity的WelcomeActivity,删除act_welcome中多余的控件,只保留根布局。

act_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".general.welcome.WelcomeActivity">

</android.support.constraint.ConstraintLayout>

第二步: 在styles.xml中添加一个自定义style,设置窗口背景为欢迎页图片,并继承AppTheme(一定要继承AppTheme,否则运行会报错——You need to use a Theme.AppCompat theme (or descendant) with this activity.

styles.xml
 <style name="Welcome" parent="AppTheme">
        <item name="android:windowBackground">@mipmap/welcome</item>
 </style>

第三步: 在AndroidMenifest.xml中为WelcomeActivity设置Theme,theme内容为@style/Welcome(注意只能为WelcomeActivity设置主题,若设置到Application下,那么每个Activity都会有背景

AndroidMenifest.xml
<activity
     android:name=".general.welcome.WelcomeActivity"
     android:theme="@style/Welcome"
     android:windowSoftInputMode="adjustNothing">
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
</activity>

第四步: 在WelcomeActivity中设置Handler,实现延时效果

WelcomeActivity.java
package com.example.reconfigmvp.general.welcome;

import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;

import com.example.reconfigmvp.general.home.HomeActivity;

public class WelcomeActivity extends BaseActivity {

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

    /**
     * 使用Handler实现延时3秒进入主页的效果
     */
    private void postDelayed() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
                startActivity(intent);
            }
        }, 3000);
    }
}

至此,一个简单的欢迎页就完成了,可以利用延时3秒的时间间隙进行一些判断,比如是否已经登录等。

猜你喜欢

转载自blog.csdn.net/m0_37063730/article/details/84579589