美文网首页Android
android之启动页面(SplashActivity)

android之启动页面(SplashActivity)

作者: Lee_5566 | 来源:发表于2020-11-27 10:25 被阅读0次
image.png

SplashActivity

打开一个应用程序时,会有一个类似欢迎的界面,它叫SplashActivity。

一般在这个页面可以做一些App数据初始化的工作。

实现的效果当用户点击App icon后,进入SplashActivity,大约经过1~2秒跳转到程序的主界面。

实战代码

SplashActivity实现全屏:

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

注意必须在setContentView前调用。

为了简单处理 这里在加载主要的active时候只做了个等待处理:

sleep(3000);

SplashActivity的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".SplashActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.977" />

</androidx.constraintlayout.widget.ConstraintLayout>

主页的active:
···
package com.exmple.splashactivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity extends BaseActive {

public static void goHome(Context context) {
    Intent intent = new Intent(context, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
}

}
···

在SplashActivity中调用:

       try {
            sleep(5000);//使程序休眠一秒
            HomeActivity.goHome(this);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

实现active的切换。

SplashActivity的全部代码:

package com.exmple.splashactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import static java.lang.Thread.sleep;

public class SplashActivity extends BaseActive {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            sleep(3000);//使程序休眠一秒
            HomeActivity.goHome(this);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

相关文章

网友评论

    本文标题:android之启动页面(SplashActivity)

    本文链接:https://www.haomeiwen.com/subject/yvpniktx.html