Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 新手入門:HelloWorld 程式碼逐行解析

編輯:關於 Android 開發

對於剛接觸 Android 的開發者而言,第一個程式往往就是 HelloWorld。以下就來逐一解析這個入門範例所涉及的幾個檔案。

一、HelloWorld 類別

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

它繼承了 Activity。可以把一個 Activity 想像成手機的螢幕,用於展示一屏的內容,因此所有要顯示內容的螢幕都必須繼承 Activity 才能實現。接著它覆蓋了 onCreate() 方法來對該 Activity 進行初始化,並以 setContentView(R.layout.main) 指定使用 main.xml 這個版面配置檔作為當前 Activity 的內容。main.xml 放在 res/layout 目錄下,我們可以直接用 R.layout.main 來引用它,而無需再透過 File 類別去讀取——只要把 XML 檔的索引交給 Android,它就會自動幫我們找到並使用,這正是 Android 相當便利之處。

二、main.xml 版面配置檔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
    />
</LinearLayout>

內容不多,開頭是一個 LinearLayout 元件的定義,並在其中放入一個用於顯示文字的 TextView。幾個參數說明如下:

  • LinearLayout 是一個線性布局面板,只支援垂直與水平兩種排列方式。android:orientation="vertical" 代表子元素垂直排列,設為 "horizontal" 則為水平排列。
  • android:layout_width 定義當前視圖佔據的寬度,此處為 fill_parent 即充滿整個螢幕;設為 wrap_content 則會依據視圖內容自動調整寬度。
  • android:layout_height 定義視圖高度,此處同樣填滿整個螢幕;設為 wrap_content 則依內容自動調整高度。
  • android:text 指定 TextView 要顯示的文字,可以是字串,也可以是字串資源的引用。此處是一個引用,指向 strings.xml 中定義、名稱為 hello 的字串。

三、strings.xml 字串資源

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloWorld!</string>
    <string name="app_name">HelloWorld</string>
</resources>

顧名思義,這裡定義了一組組鍵值對,供其他地方使用(例如 main 中對 hello 的引用)。這種做法對於字串的统一管理與國際化(i18n)有著重要意義。

四、AndroidManifest.xml 說明

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.flysnow"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloWorld"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />
</manifest>

這是專案的重要設定檔,諸如 Activity、權限、Intent 等都在此配置。各標籤含義如下:

  • package:定義該應用的套件名稱。
  • android:versionCode:定義應用的版本號。
  • android:versionName:定義應用的版本名稱。
  • application:定義一個應用,每個專案最多只有一個 Application 標籤。其中 android:icon 定義應用圖示,android:label 定義應用名稱。
  • activity:定義一個 Activity,你的每個 Activity 都必須在此定義,否則無法運行。android:name 定義 Activity 的類名,此處的 .HelloWorld 是以上面的 package 為基礎,也就是 package(com.flysnow)加上 android:name(.HelloWorld)要能定位到 com.flysnow.HelloWorld 這個 Activity。android:label 定義該 Activity 的標題。
  • intent-filter:定義一個 Intent 過濾器,用於標識對應的 Activity,讓 Android 系統能找到它。它定義的是隱性 Intent,主要透過 action 與 category 兩個子標籤來區分。
  • <uses-sdk android:minSdkVersion="8" />:定義應用的最低 SDK 級別。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved