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

五個立刻能用的技巧,讓 Android 專案告別複製貼上

編輯:關於 Android 程式設計

剛入門的時候,遇到相似需求的第一反應往往是複製貼上。短期看來很快,長期卻會累積出可觀的工作量與維護成本:同一段邏輯散落在十個檔案裡,改一處就得改十處。

以下整理五種提升 Android 程式碼 reuse 的做法,由淺入深。為了容易理解,例子都刻意寫得簡單。

一、活用 include:版面配置的 reuse

include 顧名思義就是「包含」。最典型的場景是 App 的標題列——每個頁面的標題區結構幾乎一樣,卻要在每個 XML 裡重寫一遍。

先建立 layout/include_title.xml:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/blue">

    <ImageButton
        android:id="@+id/imgbtnback"
        android:layout_width="40dp"
        android:layout_height="fill_parent"
        android:background="@color/transparent"
        android:src="@drawable/fanhui" />

    <TextView
        android:id="@+id/tvtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="標題"
        android:textColor="@color/white"
        android:textSize="20dp" />
</RelativeLayout>

接著在任何需要的頁面裡一行帶入:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/include_title" />
</LinearLayout>

二、用 extends:把 UI 效果收進 BaseActivity

有些功能幾乎每個頁面都要用,例如 Toast、Dialog。與其在各處重寫,不如封裝進一個基底 Activity,需要時繼承它、直接呼叫方法。

import android.app.Activity;
import android.widget.Toast;

public class BaseActivity extends Activity {

    /** 彈出提示(string 資源 id) */
    public void showToast(int intContent) {
        Toast.makeText(getApplication(), intContent, Toast.LENGTH_SHORT).show();
    }

    /** 彈出提示(字串) */
    public void showToast(String strContent) {
        Toast.makeText(getApplication(), strContent, Toast.LENGTH_SHORT).show();
    }
}

之後任何頁面只要 extends BaseActivity,就能直接 showToast("...")。同名方法的不同參數版本(overloading)讓呼叫端不必記兩套 API。

三、類別封裝:把功能邏輯抽成 Model 層

第二點談的是畫面效果的 reuse,這裡談的是功能邏輯的 reuse。當某個運算或處理被反覆呼叫時,把它抽成獨立類別,就是 MVC 中的 Model 層。

/** 工具類 */
public class Tools {
    public static void outPut(Object obj) {
        System.out.println(obj);
    }
}

需要時直接 Tools.outPut(obj)。用 static 是因為這類方法通常無狀態;若某工具需要持有狀態(例如快取),就該改成單例或交由依賴注入管理。

四、把常數收進 string.xml 與 color.xml

開發時難免會用到大量顏色值與文字。把它們散落在各個配置檔裡,日後改需求就得全專案搜尋;集中管理則改一處即可,同時也方便做多語系與主題切換。

<!-- res/values/color.xml -->
<resources>
    <color name="transparent">#00000000</color>
    <color name="black">#000000</color>
    <color name="blue">#5DC0F8</color>
</resources>

使用時寫 @color/blue 或 @string/app_name,IDE 也會對這些資源名稱提供 autocomplete 與錯誤檢查。

五、抽成 library:跨專案的 reuse

做到第三、四個專案時你會發現,很多功能與視覺效果彼此高度相似。這時候就該把這些基本程式碼收攏成一個 library module 或 AAR,之後新專案直接依賴,不必重寫。 conceptually 這跟引入開源框架是同一件事,只是來源換成自己的資產。

怎麼取捨

  • 版面重複 → include(同一個專案內);樣式屬性重複 → style。
  • UI 行為重複 → BaseActivity / BaseFragment。
  • 純邏輯重複 → 獨立工具類或 manager。
  • 字面值重複 → 資源檔。
  • 跨專案重複 → library。

過度抽象也是一種成本。 reuse 的判斷標準很簡單:當你第二次複製貼上同一段程式時,就是該抽出来的时候。

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