作为APP程序,为了界面美观,提高用户体验,在进入主界面之前都会有个页面,经常放广告,或者美观的图片,提升用户的评价,让客户喜欢上这款APP,我们称之为闪屏页.
闪屏页的使用
闪屏页使用起来很简单,需要用到handler的一个方法:postDelayed(),里面有2个参数,一个是Runnable接口,一个是延迟执行的时间参数。
闪屏页的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher_background"
>
<LinearLayout
android:id="@+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_marginBottom="200dp"
>
<ImageView
android:id="@+id/splash_icon"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/tu_biao" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="备忘录"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
进入的主界面没内容就只有一个TextView
主界面的代码和布局都没用,所以就不粘贴了
闪屏页的代码
package com.example.administrator.csdn_splash;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Splash_activity extends AppCompatActivity {
protected Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash_activity.this,MainActivity.class));
finish();
}
},3000);
}
}
可以看到run方法里直接用Intent跳转到主界面,后面的3000毫秒也就是3秒 代表这个线程延迟3秒执行。
最后的效果就是3秒后跳转主界面
闪屏页虽然简单,但是应用广泛,用熟练它很重要。
作者:谭健
原文链接: :[点击这里](https://blog.csdn.net/weixin_39028072/article/details/80637987)