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

Android 應用程序文件結構詳解:資源描述文件與邏輯代碼

編輯:關於 Android 開發

延續上一篇的 HelloWorld 項目,本文將深入分析 Android 應用程序由哪些文件構成。可以這樣理解:Android 應用通過 Java 代碼實現業務邏輯,由 XML 文件描述界面及其他一切資源。下面我們來看幾類關鍵文件。

一、資源及其描述文件

1. strings.xml 文件

定義程序中使用的字符串資源,位於 res/values 目錄下。新建項目時自動生成的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">搜投網</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

其中每個 string 標籤定義一個字符串常量,該變量可在 Java 和 XML 文件中通過 @string/ 引用。這些引用可在 AndroidManifest.xml 中找到對應的使用。

2. styles.xml 文件

用於預先定義布局中需要顯示的樣式,如文本顏色和字體等。通過 style 標籤的 name 指定樣式名稱,parent 指定繼承項:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#111</item>
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:textSize">8sp</item>
    </style>
</resources>

可以看到,AppTheme 在繼承父樣式其他屬性不變的情況下,覆寫了文本大小為 8sp。

3. dimens.xml 文件

通常用於定義布局常量,在樣式和布局資源中定義邊距、高度和尺寸時經常會用到。使用 <dimen> 標籤指定一個維度資源。常見的維度單位包括:

px(像素)、in(英寸)、mm(毫米)、pt(磅,1/72 英寸)、dip(與密度無關的像素)、sp(與刻度無關的像素)。

建議:文字使用 sp 作為單位,其他元素使用 dip 作為單位。

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

4. colors.xml 文件

使用 <color> 標籤定義顏色資源。顏色值由 RGB(三位十六進制)或 RRGGBB(六位十六進制)表示,以 # 符號開頭,如 #00f(藍色)、#00ff00(綠色)。定義透明色時,透明度 alpha 通道值緊隨 # 之後,如 #7700ff00(透明綠色)。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="listDivider">#ffcc99</color>
    <color name="character">#f37301</color>
</resources>

5. activity_main.xml 文件

位於 res/layout 子文件夾中,定義第一個顯示界面的布局(默認)。其中 xmlns:android 是 XML 的命名空間,告知 Android 工具相關公共屬性已在該命名空間中定義:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

6. AndroidManifest.xml 文件

位於根目錄的清單文件,用於應用程序的全局描述,定義了包名、版本、圖標、主題以及首個執行的 Activity 等:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.souvc.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.souvc.helloworld.MainActivity"
            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>
</manifest>

二、邏輯代碼文件

1. MainActivity.java 文件

位於 src 文件夾中,應用程序的操作控制部分在此定義。其中 onCreate() 是第一次創建 Activity 時的回調方法,setContentView() 用於設置當前顯示的布局:

package com.souvc.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);// 調用父類構造函數
        setContentView(R.layout.activity_main);// 設置當前顯示的布局
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

2. R.java 文件

由 Android ADT 自動生成,不能直接修改。它以資源 id 的形式標注 drawable、layout、values 文件夾中的資源信息。當在 XML 中寫入注釋後,該注釋會自動同步到 R.java 對應的資源項上。這個文件讓我們能夠在代碼中通過 R.xxx.yyy 的方式引用各類資源。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved