在Android中创建启动界面
16lz
2021-12-04
1、制作一张启动图片splash.png,放置在res->drawable-hdpi文件夹中。
2、新建布局文件splash.xml
<? xmlversion="1.0"encoding="utf-8" ?>< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent" android:layout_height ="fill_parent"
android:gravity ="bottom|center" android:orientation ="vertical"
android:background ="@drawable/splash" >
< TextView android:text ="@+id/TextView01" android:layout_width ="wrap_content"
android:layout_height ="wrap_content" android:layout_marginTop ="20dip"
android:typeface ="sans" android:shadowDx ="0" android:shadowDy ="2"
android:shadowRadius ="1" android:shadowColor ="#FFFFFF"
android:textColor ="#444444" android:textSize ="20dip" android:id ="@+id/versionNumber"
android:gravity ="bottom" >
</ TextView >
</ LinearLayout >
这里我们把上一步制作的图片作为启动界面的背景图,然后在界面底部显示当前程序的版本号。
3、新建SplashActivity,在Oncreate中添加以下代码:
setContentView(R.layout.splash);
PackageManagerpm = getPackageManager();try {
PackageInfopi = pm.getPackageInfo( " com.lyt.android " , 0 );
TextViewversionNumber = (TextView)findViewById(R.id.versionNumber);
versionNumber.setText( " Version " + pi.versionName);
} catch (NameNotFoundExceptione){
e.printStackTrace();
}
new Handler().postDelayed( new Runnable(){
@Override
public void run(){
Intentintent = new Intent(SplashActivity. this ,SplashScreenActivity. class );
startActivity(intent);
SplashActivity. this .finish();
}
}, 2500 );
4、 修改Manifest文件,将启动界面Activity改为默认启动,并且设置标题栏不可见。
<?xmlversion="1.0"encoding="utf-8"?>
< manifestxmlns:android = " http://schemas.android.com/apk/res/android "package = " com.lyt.android " android:versionCode = " 1 " android:versionName = " 1.0 " >
< applicationandroid:icon = " @drawable/icon " android:label = " @string/app_name " >
< activityandroid:name = " .SplashActivity " android:label = " @string/app_name "
android:theme = " @android:style/Theme.NoTitleBar " >
< intent - filter >
< actionandroid:name = " android.intent.action.MAIN " />
< categoryandroid:name = " android.intent.category.LAUNCHER " />
</ intent - filter >
</ activity >
< activityandroid:name = " .SplashScreenActivity " android:label = " @string/app_name " >
</ activity >
</ application >
< uses - sdkandroid:minSdkVersion = " 8 " />
</ manifest >
5、显示效果如下:
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android(安卓)Adapter详解
- Android(安卓)Studio启动时报错 “ Plugin "XXX" was not loaded
- android studio3.6.3编译Telegram android 1482 问题小记(nebula
- Android(安卓)HttpClient post MultipartEntity - Android(安卓)
- debug.keystore过期问题
- Android在开机时自动启动一个应用程序
- Android之常用文件路径