这里用两种方式实现应用的启动页面:
1,使用Handler的postDelayed(runnable, delayTime)方法:
将runnable对象加入handler queue,当经过delayTime后,runnable会运行在handler所绑定的线程上。
2,使用定时器:
package com.example.demo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcom_layout);
welcom();
}
private void welcom() {
/*new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(mainIntent);
WelcomeActivity.this.finish();
}
}, 2000);*/
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(mainIntent);
WelcomeActivity.this.finish();
}
}, 2000);
}
}
这里只是用了一张图片作为应用的启动画面,当然想要做得更炫的话,可以用动画之类的:
<?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" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/huoying" />
</RelativeLayout>
启动之后的应用主界面:
package com.example.demo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_world" />
</LinearLayout>
在manifest中,我们设置了activity 的 android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"属性,这样这个欢迎页面就会全屏显示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.demo.MainActivity"
android:label="@string/app_name" >
</activity>
<activity android:name="com.example.demo.WelcomeActivity"
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>
</application>
</manifest>
下面再说一下界面全屏的设置方法:
1,在manifest中设置activity / application 的 android:theme属性:
设置为:@android:style/Theme.Black.NoTitleBar 这样并非是全屏,但是不会显示actionBar;
设置为:@android:style/Theme.Black.NoTitleBar.Fullscreen 这样就会全屏显示
(还可以设置:android:screenOrientation="landscape" 屏幕就会横屏显示了)
2,在代码中设置:
//不显示ActionBar
requestWindowFeature(Window.FEATURE_NO_TITLE);
//不显示系统的通知栏()全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
一定要记住要在setContentView()之前设置窗口的信息。