編輯:關於 Android 程式設計
多數 Android 介面都在 XML 中預先配置完成,但有些情境天生無法靜態定義——最典型的就是聊天室。訊息數量隨對話增加,不可能事先寫死,這時候就必須在執行期以程式碼動態建立控制項。
好消息是,Android 的 UI 從來不只有 XML 一種途徑。所有 View 與 ViewGroup 都能用 Java 直接 new 出來,再透過 addView() 組裝成畫面。
動態建立時最容易忽略的是版面參數。每個控制項都需要一組 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 以分別設定字級與樣式。
Android 設備連接 Unity Profiler 性能分析器
Unity 提供了兩種方式,讓開發者的 Android 設備連接到 Profiler 進行性能分析: 1. 通過 WiFi,讓 Android 設備和計算機處於同一個 W
Android 自訂控制項輕鬆實現側滑菜單效果
側滑菜單在許多應用中都會見到,最近 QQ 5.0 的側滑還玩了點花樣。對於側滑菜單,一般人通常會自訂 ViewGroup,然後隱藏菜單欄,手指滑動時透過 Scroller
Android Studio 如何配置 JNI/NDK 開發環境
安裝 NDK 在 Android Studio 中開發 JNI 前,需要先確認 NDK 已安裝。可開啟 SDK Manager,在 SDK Tools 中勾選 NDK,下
【首發】在 Android Studio 中輕鬆搭建 JavaCV 開發環境
最近參加一個比賽需要用到人臉識別,但賽方限制了不能調用第三方雲服務 API,於是我想到了用 JavaCV 來自己實現。可搜索下來,絕大多數教程都是在 Eclipse 裡搭