01-android studio实现闪屏页功能

在Android开发中,闪屏页面是指应用程序启动时展示的第一个页面,通常用来展示应用的logo或者介绍信息,让用户在应用加载完毕前看到一个友好的界面。本文将介绍如何实现一个简单的Android闪屏页面。

 一、创建一个新的SplashActivity

首先,在Android Studio中创建一个新的Activity作为闪屏页面。可以在res/layout文件夹下创建一个新的布局文件作为闪屏页面的布局。

二、编辑activity_splash.xml

在闪屏页面的布局文件中,可以添加一个ImageView用来展示logo,或者添加一些文本信息作为介绍。可以根据实际需求设计界面布局(我这里用的是一张图片)。

<?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">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

三、编辑SplashActivity.java

闪屏页面一般只需要显示几秒钟,然后跳转到应用的主界面。可以通过定时器来实现闪屏页面的显示时长。

通过意图跳转到MainActivity(后面我会改成登录页)

package com.example.test01;

import androidx.appcompat.app.AppCompatActivity;

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

public class SplashActivity extends AppCompatActivity {

    private static final int SPLASH_DISPLAY_LENGTH = 2000; // 闪屏页面显示时长

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

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(mainIntent);
                finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

注意

要在 AndroidManifest.xml 把SplashActivity.java设置为默认打开

<!--        将闪屏页作为首页-->
        <activity
            android:name=".SplashActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

补充: 去掉标题栏

在需要去除标题栏的Activity里面的onCreate()方法中加入下面一段代码 

        //去除默认标题栏
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

参考链接:
https://blog.51cto.com/u_16213333/9980860

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值