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

Android 動態產生控制項:純 Java 打造聊天介面

編輯:關於 Android 程式設計

多數 Android 介面都在 XML 中預先配置完成,但有些情境天生無法靜態定義——最典型的就是聊天室。訊息數量隨對話增加,不可能事先寫死,這時候就必須在執行期以程式碼動態建立控制項。

好消息是,Android 的 UI 從來不只有 XML 一種途徑。所有 View 與 ViewGroup 都能用 Java 直接 new 出來,再透過 addView() 組裝成畫面。

核心概念:LayoutParams

動態建立時最容易忽略的是版面參數。每個控制項都需要一組 LayoutParams 來描述自己的寬高與對齊方式,常見的有三種組合:

private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

FILL_PARENT(後改名為 MATCH_PARENT)表示佔滿父容器,WRAP_CONTENT 則只包住自身內容。

完整程式碼

package com.fetion.android;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
import java.util.Calendar;

public class fetion2009 extends Activity {
    ProgressBar pb; // 拿出來是為了能動態改變進度

    // 聊天對話底色採間隔排列
    private static final int[] bg = { Color.WHITE, Color.GRAY };
    private static int bgIndex = 0;

    private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

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

        // 對話需要捲動,外層包一個 ScrollView
        ScrollView sv = new ScrollView(this);
        sv.setLayoutParams(LP_FF);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(0xff00ffff); // 特意設一個顯眼顏色,方便檢查版面是否填滿

        for (int i = 0; i < 10; i++) {
            setSendMsg(layout, this, getCurrColor(), i + "聊天內容在這裡。。");
        }
        // 圓環進度條(ProgressBar 預設樣式)
        setSendFile(layout, this, getCurrColor(), "我的照片.jpg");
        // 水平進度條
        setSendFile2(layout, this, getCurrColor(), "我的照片.jpg");

        for (int i = 0; i < 10; i++) {
            setSendMsg(layout, this, getCurrColor(), i + "聊天內容在這裡。。");
        }

        sv.addView(layout);
        setContentView(sv);
    }

    /** 取得目前對話的底色,兩色交替 */
    private int getCurrColor() {
        return bg[(++bgIndex) % bg.length];
    }

    /** 動態新增一則聊天內容 */
    private void setSendMsg(LinearLayout layout, Context context, int bgColur, String MSG) {
        TextView tv = new TextView(context);
        tv.setText("某人 說: [" + DateFormat.format("kk:mm", Calendar.getInstance()) + "]\n" + MSG);
        tv.setBackgroundColor(bgColur);
        layout.addView(tv);
    }

    /** 動態新增一則「傳送檔案」項目(圓環進度條) */
    private void setSendFile(LinearLayout layout, Context context, int bgColur, String fileName) {
        setSendMsg(layout, context, bgColur, "正在發送" + fileName);

        // 進度條與取消鈕要水平排列,需要再包一層 LinearLayout
        LinearLayout myLayout = new LinearLayout(context);
        myLayout.setBackgroundColor(bgColur); // 不設會透出底層的青色

        ProgressBar pb = new ProgressBar(context);
        pb.setLayoutParams(LP_WW);
        myLayout.addView(pb);

        Button bt = new Button(context);
        bt.setLayoutParams(LP_WW);
        bt.setText("取消");
        myLayout.addView(bt);

        layout.addView(myLayout);
    }

    /** 動態新增一則「傳送檔案」項目(水平進度條) */
    private void setSendFile2(LinearLayout layout, Context context, int bgColur, String fileName) {
        setSendMsg(layout, context, bgColur, "正在發送" + fileName);

        LinearLayout myLayout = new LinearLayout(context);
        myLayout.setBackgroundColor(bgColur);
        myLayout.setOrientation(LinearLayout.VERTICAL);

        // ProgressBar 預設是圓環,這裡改成水平樣式
        pb = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        pb.setLayoutParams(LP_FW);
        pb.setProgress(45);
        pb.setSecondaryProgress(0);
        myLayout.addView(pb);

        Button bt = new Button(context);
        bt.setLayoutParams(LP_WW);
        bt.setText("取消");
        myLayout.addView(bt);

        layout.addView(myLayout);
    }

    /** 用方向鍵控制進度條 */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                pb.setProgress(pb.getProgress() - 5);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                pb.setProgress(pb.getProgress() + 5);
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}

歸納一下

動態建立控制項的三個要點:先準備好 LayoutParams、用 addView() 組裝、需要水平排列時記得多包一層 LinearLayout。這個範例把「某人說 + 時間 + 內容」塞進同一個 TextView 是為了簡化,實務上通常會拆成多個 TextView 以分別設定字級與樣式。

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