Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

動態載入 XML 版面與自訂 Layout 控件的兩種實作方式

編輯:初階開發

近來因為專案需求,重寫了不少 Android 控件。寫到最後會發現目標其實就那幾個字:高效、輕巧、清晰、標準化。以下整理動態載入 Layout 的兩種做法,可依專案風格挑選。

(本文原轉自 https://blog.csdn.net/lzx_bupt/archive/2010/05/17/5600187.aspx )

方法一:靜態主 Layout 動態載入靜態子 Layout

步驟 1:建立子 Layout(main2.xml)

這裡以線性版面為例,裡面放兩個按鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/menubar"
    android:background="@drawable/menubar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageButton android:id="@+id/button1"
        android:src="@drawable/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageButton android:id="@+id/button2"
        android:src="@drawable/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

步驟 2:建立主 Layout(main.xml)

關鍵在於主版面要替子版面預留一個容器 box,並在這裡指定它的位置:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">

    <!-- 這個容器就是子 Layout 的落腳處,位置在此指定 -->
    <LinearLayout android:id="@+id/box"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
    </LinearLayout>
</RelativeLayout>

步驟 3:在程式中載入子 Layout

public class BackgroundTest extends Activity {
    // 子 Layout 要以 View 形式加入主 Layout
    private View mBarView;
    // 主 Layout 中負責承載的容器
    private LinearLayout mLinearLayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);                 // 顯示主 Layout
        mBarView = View.inflate(this, R.layout.main2, null);  // 載入子 Layout
        mLinearLayout = (LinearLayout) findViewById(R.id.box);  // 取得容器
        mLinearLayout.addView(mBarView);               // 掛上 View
    }
}

方法二:靜態主 Layout 搭配自訂的動態 Layout 類別

步驟 1:先定義自己的 Layout 類別

public class MenuLandscapeLinearLayout extends LinearLayout {

    public MenuLandscapeLinearLayout(Context context) {
        super(context);
        // 在這裡載入需要的屬性,重用方法一的子 Layout
        ((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this);
        // 後續可以在此封裝各種自訂方法
    }
}

步驟 2:在程式中動態 new 出來並掛載

public class BackgroundTest extends Activity {
    private LinearLayout mLinearLayout;
    // 宣告子 Layout 的 View 物件
    private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);                // 載入主 Layout
        mLinearLayout = (LinearLayout) findViewById(R.id.box);   // 找到容器
        mMenuLandscapeLinearLayout = new MenuLandscapeLinearLayout(this);  // 實例化子 View
        mLinearLayout.addView(mMenuLandscapeLinearLayout);       // 加入容器
    }
}

兩種方式的取捨

方法一直觀易懂,適合版面簡單的情況;方法二把子版面封裝成一個類別,能把常用事件、資源整理都收進去,複用性更高,也更容易維護。

實際上可思考的空間還很多:把重複的事件綁定、資源載入邏輯抽出來封裝,不僅能省下大量重複程式碼,也能讓版面結構更乾淨。這正是重寫控件時最值得投入的部分。

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