編輯:關於 Android 程式設計
標籤、按鈕數量不固定,又希望它們能自動換行排列——這正是 FlowLayout(流式版面)要解決的問題。做法並不難,關鍵是覆寫 ViewGroup 的兩個方法:onMeasure 決定子 View 多大,onLayout 決定子 View 擺在哪。

1. 覆寫 onMeasure(int widthMeasureSpec, int heightMeasureSpec),決定每個子 View 的尺寸。
2. 覆寫 onLayout(boolean changed, int l, int t, int r, int b),逐一計算並擺放子 View 的位置。
package com.rong.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* 卡片布局
*/
public class FlowLayout extends ViewGroup {
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 當前子 View 的數量
int childSize = getChildCount();
// 獲取行寬
int lineWidth = getMeasuredWidth();
// 當前是第幾行
int lines = 1;
// 當前累加的行寬
int nowLineWidth = 0;
for (int i = 0; i < childSize; i++) {
View view = getChildAt(i);
// 子 View 的寬度與高度
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
// 如果 nowLineWidth + childWidth >= lineWidth 則換行
if (nowLineWidth + childWidth >= lineWidth) {
nowLineWidth = 0;
lines = lines + 1;
}
// 設置子 View 的位置
view.layout(nowLineWidth, childHeight * (lines - 1),
nowLineWidth + childWidth, childHeight * lines);
nowLineWidth = nowLineWidth + childWidth;
// 如果 nowLineWidth >= lineWidth 則換行
if (nowLineWidth >= lineWidth) {
nowLineWidth = 0;
lines = lines + 1;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 設置自己 View 的大小
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
// 設置每個子 View 的大小
view.measure(view.getMeasuredWidth(), view.getMeasuredHeight());
}
}
}
把一堆文字長度不一的 Button 丟進 FlowLayout,就能看到自動換行的效果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical" >
<com.rong.activity.FlowLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Apple" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cup" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Double" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ear" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flower" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Game" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hotdog" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="interseting" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="joker" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="king" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="mother" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="lost" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="noting" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="orange" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="poker" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="qustion" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ring" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="string" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="type" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="unit" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="vertion" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="west" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="x" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="young" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="zip" />
</com.rong.activity.FlowLayout>
</RelativeLayout>
這份示範程式為了聚焦在排列邏輯,假設所有子 View 等高,因此換行判斷寫得相當精簡。若你的情境需要處理「行高不一」或「左右留白對齊」,只要把 lines 改成記錄每行高度與目前的 y 座標,就能擴充成更完整的版本。
延伸閱讀:《Android 開發入門與進階教程》《Android 基本組件用法總結》《Android 視圖 View 技巧總結》《Android 布局 layout 技巧總結》《Android 控件用法總結》。
Android Handler 的用法:post 與 sendMessage 兩條路
Android 開發裡有個場景幾乎人人都撞過:界面上點一下「下載」,然後要去跑網絡請求。這是妥妥的耗時操作,誰也不知道多久能回來,所以得開新線程去做,避免阻塞 UI 線程
Android 專案自定義頂部標題欄
自定義標題欄的目的在 Android 專案中,頂部標題欄經常需要顯示返回按鈕、標題文字與提交按鈕。若每個頁面都重複撰寫標題欄邏輯,維護成本會很高。因此,可以將標題欄抽成獨
Android 接入支付寶支付的實作指南
接入前的準備 在 Android 應用中接入支付寶之前,商戶通常需要先完成簽約與審核,取得 PID,並建立 RSA 金鑰組。私鑰用於產生數字簽名,公鑰則交由支付寶平台驗證
Activity 之間傳遞 Bitmap:三種資料擺放策略與各自的代價
要在兩個 Activity 之間傳遞圖片,最直覺的做法是 intent.putExtra(key, bitmap)。但這條路限制很多——Bitmap 實現的是 Parce