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

Android 動態載入版面配置檔實作示例

編輯:關於 Android 程式設計

Android 的介面有兩種寫法:用 XML 宣告,或用 Java 硬寫。前者好維護,後者靈活但可讀性差。實務上更聰明的做法是兩者混用——把可重用的區塊寫成 XML,需要時再用 LayoutInflater 把它動態掛到畫面上。

第一步:準備一個可重用的 XML 片段

先把要動態插入的內容獨立成 res/layout/part.xml,它並不是完整的畫面,而是一塊「零件」。

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="添加" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>

</RelativeLayout>

第二步:用程式碼組裝畫面

package com.example.codeui;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);   // 顯示方向

        // 純程式碼建立的 view
        TextView textView = new TextView(this);
        textView.setText("Hello Code UI");
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.addView(textView, params);

        // 加入外部 XML 定義的版面
        View view = getPartView();
        layout.addView(view);

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        setContentView(layout, layoutParams);

        // setContentView(R.layout.activity_main);
        // 純程式碼編寫效率高,但很難維護
    }

    // 透過載入 XML 把 view 加入版面
    public View getPartView() {
        // 取得系統的 LayoutInflater 服務
        LayoutInflater inflater =
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 第二個參數是暫時掛載的父容器,這裡沒有父元素所以傳 null
        return inflater.inflate(R.layout.part, null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

重點整理

  • LayoutInflater 是 XML 與 View 物件之間的橋樑,透過 getSystemService(Context.LAYOUT_INFLATER_SERVICE) 取得。
  • inflate() 的第二個參數用來指定暫時的父容器;若這塊版面之後才掛上,傳 null 即可。
  • 動態加入子檢視時記得帶上 LayoutParams,否則寬高會出現非預期的結果。
  • 純程式碼建立 UI 執行效率高,卻難以維護;XML 剛好相反。依情境混用最合適。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved