1、新建一个AndroidActivity,会同时新建一个.xml
名称命名为StartActivity.java
2、其中StartActivity.java的内容为:
package com.example.jj;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
public class StartActivity extends Activity {
//延迟3秒
private static final long SPLASH_DELAY_MILLIS = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity
new Handler().postDelayed(new Runnable() {
public void run() {
goHome();
}
}, SPLASH_DELAY_MILLIS);
}
private void goHome() {
Intent intent=null;
intent = new Intent(StartActivity.this, MainActivity.class);
StartActivity.this.startActivity(intent);
StartActivity.this.finish();
}
}
3、对应xml的文件内容为:
<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"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/startbg"
android:scaleType="fitCenter" />
</RelativeLayout>
4、在/test/AndroidManifest.xml内容进行修改,在MainActivity前面,即是第一个界面的前面进行添加:
<activity
android:name=".StartActivity"
android:configChanges="keyboardHidden"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
5、需要注意的是:
(1)如果粘贴过去出现R文件报错时,不能直接对R文件进行修改,需要刷新一下,然后选择Project-》Clean一下,重新加载一下R文件。
(2)如果需要修改启动页图片时,记得不要出现中文符号,包括名称中“-”一定改为“_”英文的,不然报错。
(3)修改/test/AndroidManifest.xml记得放在第一个页面的前面。