创建Android启动界面

来源:互联网 发布:软件接口设计 编辑:程序博客网 时间:2024/04/29 11:54
 制作一张启动图片splash.png,放置在res->drawable-hdpi文件夹中。
        新建布局文件splash.xml

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4. android:gravity="bottom|center" android:orientation="vertical"
  5. android:background="@drawable/splash">
  6. <TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
  7. android:layout_height="wrap_content" android:layout_marginTop="20dip"
  8. android:typeface="sans" android:shadowDx="0" android:shadowDy="2"
  9. android:shadowRadius="1" android:shadowColor="#FFFFFF"
  10. android:textColor="#444444" android:textSize="20dip" android:id="@+id/versionNumber"
  11. android:gravity="bottom">
  12. </TextView>
  13. </LinearLayout>
复制代码

       这里我们把上一步制作的图片作为启动界面的背景图,然后在界面底部显示当前程序的版本号。
  新建SplashActivity,在Oncreate中添加以下代码:

java代码:
  1. setContentView(R.layout.splash);

  2. PackageManager pm = getPackageManager();
  3. try {
  4. PackageInfo pi = pm.getPackageInfo("com.lyt.android", 0);
  5. TextView versionNumber = (TextView) findViewById(R.id.versionNumber);
  6. versionNumber.setText("Version " + pi.versionName);
  7. } catch (NameNotFoundException e) {
  8. e.printStackTrace();
  9. }

  10. new Handler().postDelayed(new Runnable(){

  11. @Override
  12. public void run() {
  13. Intent intent = new Intent(SplashActivity.this,SplashScreenActivity.class);
  14. startActivity(intent);
  15. SplashActivity.this.finish();
  16. }

  17. }, 2500);
复制代码

       4、 修改Manifest文件,将启动界面Activity改为默认启动,并且设置标题栏不可见。

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package=" " android:versionCode="1" android:versionName="1.0">
  4. <application android:icon="@drawable/icon" android:label="@string/app_name">

  5. <activity android:name=".SplashActivity" android:label="@string/app_name"
  6. android:theme="@android:style/Theme.NoTitleBar">
  7. <intent-filter>
  8. <action android:name="android.intent.action.MAIN" />
  9. <category android:name="android.intent.category.LAUNCHER" />
  10. </intent-filter>
  11. </activity>
  12. <activity android:name=".SplashScreenActivity" android:label="@string/app_name" >
  13. </activity>
  14. </application>
  15. <uses-sdk android:minSdkVersion="8" />

  16. </manifest>
复制代码

原创粉丝点击