1、製作一張啟動圖片splash.png,放置在res->drawable-hdpi檔案夾中。
2、建立布局檔案splash.xml
<?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/splash" >
<TextView
android:id="@+id/versionNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="26dp"
android:layout_marginRight="52dp"
android:text="version 1.0"
android:textColor="" />
</RelativeLayout>
這裡我們把上一步製作的圖片作為啟動介面的背景圖,然後在介面底部顯示當前程式的版本號碼。
3、建立SplashActivity,在Oncreate中添加以下代碼:
PackageManager pm = getPackageManager();try { PackageInfo pi = pm.getPackageInfo("com.lyt.android", 0); TextView versionNumber = (TextView) findViewById(R.id.versionNumber); versionNumber.setText("Version " + pi.versionName);} catch (NameNotFoundException e) { e.printStackTrace();}new Handler().postDelayed(new Runnable(){@Overridepublic void run() { Intent intent = new Intent(SplashActivity.this,SplashScreenActivity.class); startActivity(intent); SplashActivity.this.finish();}}, 2500);
4、 修改Manifest檔案,將啟動介面Activity改為預設啟動,並且設定標題列不可見。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sinaweibo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity1"
android:label="@string/title_activity_main_activity1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
這樣開啟應用後等待2.5秒就進入第二個activity MainActivity了。