安卓程序一般会设置一个打开时的欢迎界面,下面我用Splash做一个微信的欢迎界面代码如下
MainActivity.java
package cn.wuxiaocheng.splash; import android.app.Activity; import android.os.Bundle; import android.view.Window; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
SplashActivity.java
package cn.wuxiaocheng.splash; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Window; public class SplashActivity extends Activity { private final int SPLASH_DISPLAY_LENGHT=3000; // 延时3秒,,可以不写这段,直接在下面SPLASH_DISPLAY_LENGHT改为延时的时间就行 //加载欢迎界面 @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题 super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); // 为了减少代码使用匿名Handler创建一个延时的调用 new Handler().postDelayed(new Runnable(){ @Override public void run() { //通过Intent打开最终真正的主界面Main这个Activity Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class); //启动Main界面 SplashActivity.this.startActivity(mainIntent); //关闭自己这个开场屏 SplashActivity.this.finish(); } }, SPLASH_DISPLAY_LENGHT); } }
activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash_welcome"/> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.wuxiaocheng.splash" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- 下面这个activity设置打开程序是第一个页面为欢迎界面--> <activity android:name="cn.wuxiaocheng.splash.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> <activity android:name="cn.wuxiaocheng.splash.MainActivity"></activity> </application> </manifest>
时间: 2024-10-14 11:27:02