1. 程式人生 > >實現安卓啟動頁設定

實現安卓啟動頁設定

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記得放在第一個頁面的前面。