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

Android 自定義 ViewGroup 實作 FlowLayout 流式版面配置(三)

編輯:關於 Android 程式設計

本篇延續自定義 ViewGroup 的系列主題,動手做一個實用的範例:FlowLayout(流式版面配置)。

所謂 FlowLayout,指的是子控制項會依序往右排入容器,一旦目前這一行剩下的寬度不夠放下一個子 View,就自動換到下一行繼續排列。因為像水流一樣逐行填滿,所以又被稱為流式版面配置。

Android SDK 本身並沒有內建流式版面配置,但在不少場景裡它卻格外好用,例如關鍵字標籤、搜尋熱詞列表、文章分類標籤等等。如下圖所示的標籤群組:

一、定義 FlowLayout

LayoutParams 的定義與 onLayout 的寫法,和上一篇 WaterfallLayout 幾乎完全一致,這裡不再重複,有興趣的讀者可以回頭參考《Android 自定義 ViewGroup(二)之 WaterfallLayout》。

真正需要花心思的是 onMeasure:我們要在測量階段一邊累加每個子 View 的寬高,一邊判斷是否需要換行,同時把每個子 View 的座標先算好存進 LayoutParams,留給 onLayout 使用。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // 取得父容器為我們設定的測量模式與尺寸
    int sizeWidth  = MeasureSpec.getSize(widthMeasureSpec);
    int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
    int modeWidth  = MeasureSpec.getMode(widthMeasureSpec);
    int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

    int childCount = getChildCount();

    // wrap_content 情況下用來記錄整體寬高
    int wrapWidth  = 0;
    int wrapHeight = 0;
    // 目前這一行累積的寬度(含右側 hSpace)
    int lineWidth  = 0;
    // 目前這一行的高度,取本行最高的子 View
    int lineHeight = 0;

    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        measureChild(child, widthMeasureSpec, heightMeasureSpec);

        LayoutParams lParams = (LayoutParams) child.getLayoutParams();
        int childWidth  = child.getMeasuredWidth();
        int childHeight = child.getMeasuredHeight();

        if (lineWidth + childWidth > sizeWidth) {
            // 放不下了,換行:記錄新行第一個標籤的座標
            lParams.left   = 0;
            lParams.top    = wrapHeight + lineHeight + vSpace;
            lParams.right  = childWidth;
            lParams.bottom = lParams.top + childHeight;

            // 注意 lineWidth 含右側 hSpace,比較時要減掉
            wrapWidth = Math.max(lineWidth - hSpace, childWidth);

            // 重新開始記錄新行
            lineWidth  = childWidth + hSpace;
            wrapHeight += lineHeight + vSpace;
            lineHeight  = childHeight;
        } else {
            // 仍在同一行,往右追加
            lParams.left   = lineWidth;
            lParams.top    = wrapHeight;
            lParams.right  = lParams.left + childWidth;
            lParams.bottom = lParams.top + childHeight;

            lineWidth += childWidth + hSpace;
            lineHeight = Math.max(lineHeight, childHeight);
        }

        // 最後一個子 View 需要收尾
        if (i == childCount - 1) {
            wrapWidth = Math.max(wrapWidth, lineWidth - hSpace);
            wrapHeight += lineHeight;
        }
    }

    setMeasuredDimension(
        (modeWidth  == MeasureSpec.EXACTLY) ? sizeWidth  : wrapWidth,
        (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : wrapHeight);
}

二、使用 FlowLayout

用法非常直觀,直接在 XML 裡把子 View 寫進去即可:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:attr="https://schemas.android.com/apk/res/com.hx.flowlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E1E6F6"
    android:orientation="vertical">

    <com.hx.flowlayout.FlowLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        attr:hSpace="20"
        attr:vSpace="10">

        <TextView android:text="標簽" />
        <TextView android:text="Welcome" />
        <TextView android:text="IT工程師" />
        <TextView android:text="程序猿" />
        <TextView android:text="Android" />
        <TextView android:text="Java" />
        <TextView android:text="ViewGroup" />
        <TextView android:text="FlowLayout" />
    </com.hx.flowlayout.FlowLayout>
</LinearLayout>

上面這種寫法雖然直白,但每個標籤都寫死在 XML 裡顯得冗長。實務上更常見的做法是透過 Java 程式碼動態 addView,資料有多少就產生多少個標籤。

接著定義兩種標籤風格,透過 style 讓外觀與版面分離:

<style name="flow_text_style_1">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/flow_text_bg_1</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">16sp</item>
</style>

<style name="flow_text_style_2">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/flow_text_bg_2</item>
    <item name="android:textColor">#4B0082</item>
    <item name="android:textSize">20sp</item>
</style>

背景使用 shapeDrawable 畫出圓角膠囊狀的標籤底色,圓角、描邊、內距都能自由調整:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="40dp"/>
    <stroke android:color="#C9C9C9" android:width="2dp"/>
    <padding android:bottom="2dp" android:left="10dp" android:right="10dp" android:top="2dp" />
</shape>

最終執行結果如下:

原始碼下載:https://xiazai.jb51.net/201609/yuanma/Android-FlowLayout(jb51.net).rar

掌握 onMeasure 中的換行判斷與座標記錄之後,FlowLayout 這類自定義 ViewGroup 就不再神祕;只要再補上 margin 支援與 padding 計算,就能延伸出更完整的標籤元件。

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