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

重寫 ViewGroup 做卡片流式版面:FlowLayout 實作解析

編輯:關於 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 的位置。

第一步:建立 FlowLayout 類別

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 控件用法總結》。

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