Android开发----实现应用启动页
写在前面:
本次实现参考糖心荷包蛋小姐姐的文章进行操作,小姐姐写的非常清楚,点击查看原文哦!
我再做个笔记。
流程步骤:
但是我做完之后存在一个小问题,虽然状态栏被隐藏掉了,但是显示状态栏背景是黑色的一个黑条。
1.创建空的启动页,Empty Activity
【项目文件夹右键—new—Activity—Empty Activity】
这里命名为SplashActivity
自动生成布局资源文件.XML和.java文件
2.设置启动的第一个页面为创建的启动页的Activity
在AndroidMainfest.XML资源文件中,设置启动页面。
<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>
3.在启动页的XMLdrawabe资源添加启动图片资源;
//添加drawable资源
//引入activity_splash.XML文件中
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash"></ImageView>
4.编辑.java文件,实现启动页持续几秒跳转至MainActivity:
隐藏状态栏、工具栏、创建新线程实现sleep3秒然后跳转
package com.domain.mainView;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//隐层状态栏和标题栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//隐藏标题栏
getSupportActionBar().hide();
setContentView(R.layout.activity_splash);
//创建子线程
Thread mThread=new Thread(){
@Override
public void run(){
super.run();
try{
sleep(3000);
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//启动线程
mThread.start();
}
}
运行结果如上图,状态栏被隐藏,但是呈现为一个黑条。