【手机安全卫士01】项目Splash页面的开发与设计

时间:2024-03-27 00:04:14

首先建立一个安卓的项目,然后修改manifest.xml文件,修改应用程序的logo和显示名称,效果图如下:

【手机安全卫士01】项目Splash页面的开发与设计

对应的代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lq.wangzhen.mobilesafe"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/callmsgsafe" //设置应用程序的logo
android:label="@string/app_name" //设置应用程序的名称
android:theme="@style/AppTheme" >
<activity
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name="lq.wangzhen.mobilesafe.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

使用到的strings.xml文件如下:

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">手机卫士</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> </resources>

更改完成应用程序的图片以后,下面开始编写应用程序的启动界面,界面的效果图如下:

【手机安全卫士01】项目Splash页面的开发与设计

这里我们包含了一下的几个信息:

  1. 当前应用程序的版本号;
  2. 启动时的一个ProgressBar;
  3. 背景图片
  4. 设置显示为全屏幕显示,没有标题栏和状态栏

建立一个SplashActivity,对应的布局文件为:activity_splash.xml文件,然后在manifest.xml文件中配置当前的Acitivity,代码如下:

 <activity
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //配置当前的Activity是全屏显示的
android:name="lq.wangzhen.mobilesafe.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> //配置当前的Activity是应用程序的启动页面 <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

编辑完成以后,完成activity_spalsh.xml文件,完成Spash页面的布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SplashActivity"
android:background="@drawable/logo2" > //配置当前Activity的背景图片 <TextView //显示当前应用程序的版本号
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_splash_version"
android:text="版本号:"
android:layout_alignParentRight="true"
android:textColor="#FF0000"
android:textSize="20sp"/> <ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="130dp"/> </RelativeLayout>

然后在SpalshActivity类中加载以上的activity_splash.xml布局文件,然后运行程序即得到以上的效果图。

 package lq.wangzhen.mobilesafe;

 import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.widget.TextView; public class SplashActivity extends Activity {
private TextView tv_splash_version;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
this.tv_splash_version = (TextView) this.findViewById(R.id.tv_splash_version);
this.tv_splash_version.setText("版本号:"+getVersion()); //设置应用程序的版本号
}
/**
* 取得应用的版本号
* @return
*/
public String getVersion(){
PackageManager pm = getPackageManager(); //取得包管理器的对象,这样就可以拿到应用程序的管理对象
try {
PackageInfo info = pm.getPackageInfo(getPackageName(), 0); //得到应用程序的包信息对象
return info.versionName; //取得应用程序的版本号
} catch (NameNotFoundException e) {
e.printStackTrace();
//此异常不会发生
return "";
}
}
}