標籤:
一、目標。
1、啟動時全螢幕顯示,並獲得當前應用版本號碼;
2、美化字型:顏色和文字陰影;
3、添加一個ProgressBar以增加使用者體驗。
效果
二、編寫過程。
1、布局。在布局檔案中增加一個TextView和一個ProgressBar,並根據要求進行布置(本例採用RelativeLayout進行布置)。
①.在RelativeLayout中的android:background屬性通過@id的方法找到drawable檔案下的布局檔案。
②.在TextView中通過android:shadowColor="RGB顏色"(設定陰影顏色)、android:shadowDx="int"(設定陰影X軸位移量)、android:shadowDy="int"(設定陰影Y軸位移量)、android:shadowRadius="int"(設定陰影半徑)。設定字型顏色為白色,預設內容為“版本1.0”,位於RelativeLayout正中間。
③.在TextView下方放置一個ProgressBar。
布局代碼如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/launcher_bg" 6 tools:context=".SplashActivity" > 7 8 <TextView 9 android:id="@+id/tv_version"10 android:layout_width="wrap_content"11 android:layout_height="wrap_content"12 android:layout_centerInParent="true"13 android:shadowColor="#00ffff"14 android:shadowDx="1"15 android:shadowDy="1"16 android:shadowRadius="1"17 android:text="版本1.0"18 android:textColor="#ffffff"19 android:textSize="22sp" />20 21 <ProgressBar22 android:layout_width="wrap_content"23 android:layout_height="wrap_content"24 android:layout_below="@id/tv_version"25 android:layout_centerHorizontal="true" />26 27 </RelativeLayout>
View Code
2、啟動樣式。程式啟動時全螢幕顯示。
在程式設定檔(AndroidManifest.xml)中,在application節點下的activity節點裡面增加android:theme屬性,其值=“@android:style/Theme.NoTitleBar.Fullscreen”。其中Theme.後還有很多樣式可選擇。
樣式代碼如下:
1 <activity2 android:name="com.example.mobilesafe.SplashActivity"3 android:label="@string/app_name"4 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
View Code
3、代碼實現版本號碼的顯示。
①.在主代碼中建立一個傳回值類型是String的private方法,取名為getVersionNumber();
②.在getVersionNumber()方法中,通過getPackageManager()方法獲得PackageManager類型的傳回值,取名pm;
③.通過PackageManager類型對象(pm)的getPackageInfo(packageName, flags)方法獲得PackageInfo類型的傳回值,取名packageInfo。在getPackageInfo(packageName, flags)方法中的參數packageName是指想要擷取包資訊的包全名,如(com.google.apps.contacts),如果想擷取本程式對應的包的全名,可用getPackageName()方法;flags是指int類型的標籤,目前可用0代替。同時,該方法會產生異常,採用try{...}catch{...}模組捕獲。
④.通過PackageInfo類型對象(packageInfo)的versionName方法可以獲得String類型的傳回值,該傳回值就是packageName的版本號碼。其實就是程式設定檔(AndroidManifest.xml)中manifest節點下的android:versionName中的值。
⑤.最後通過TextView的setText(“text”)方法更改其中的常值內容。
實現代碼如下:
1 package com.example.mobilesafe; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.pm.PackageInfo; 6 import android.content.pm.PackageManager; 7 import android.content.pm.PackageManager.NameNotFoundException; 8 import android.view.Menu; 9 import android.widget.TextView;10 11 public class SplashActivity extends Activity {12 private TextView tv_version;13 14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_splash);18 tv_version = (TextView) findViewById(R.id.tv_version);19 tv_version.setText("版本"+getVersionNumber());20 }21 22 /*23 * 得到應用程式版本號碼24 */25 26 private String getVersionNumber() {27 //用於管理手機的APK28 PackageManager pm = getPackageManager();29 //得到指定APK的功能資訊清單檔(即AndroidMainfest.xml)30 try {31 PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);32 return packageInfo.versionName;33 } catch (NameNotFoundException e) {34 e.printStackTrace();35 return "";36 } 37 }38 }View Code
三、校正。
通過更改程式設定檔(AndroidManifest.xml)中manifest節點下的android:versionName中的值可以改變啟動時的版本號碼。
Android執行個體-手機安全衛士(一)-啟動介面