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

FlexboxLayout 完全解析:主軸方向、換行與對齊屬性一圖看懂

編輯:關於 Android 開發

FlexboxLayout 是 Google 開源的 Android 佈局庫,血脈來自 CSS 裡的 Flexbox——W3C 在 2009 年提出的響應式佈局方案。對前端同學來說概念並不陌生,而 Android 開發者也能借此擺脫 LinearLayout 嵌套的地獄。

一、容器屬性

flexDirection:決定主軸方向

類似 LinearLayout 的 vertical / horizontal,可選四個值:

  • row(默認):主軸水平,起點在左。
  • row-reverse:主軸水平,起點在右。
  • column:主軸垂直,起點在上。
  • column-reverse:主軸垂直,起點在下。

row:

row-reverse:

column:

column-reverse:

flexWrap:是否換行

默認不換行,子元素會被壓縮擠在一行裡。

  • nowrap:不換行。
  • wrap:正常方向換行。
  • wrap-reverse:反方向換行。

wrap:

wrap-reverse:

justifyContent:主軸上的對齊方式

  • flex-start(默認):靠起點,左對齊。
  • flex-end:靠終點,右對齊。
  • center:居中。
  • space-between:兩端對齊,元素之間間距相等。
  • space-around:每個元素兩側間距相等(因此相鄰元素間距是單側的兩倍)。

space-around 效果:

alignItems:交叉軸上的對齊方式

  • flex-start:交叉軸起點對齊(靠上)。
  • flex-end:交叉軸終點對齊(靠下)。
  • center:交叉軸中點對齊(居中線)。
  • baseline:以第一行文字基線對齊。
  • stretch(默認):在子元素未指定高度或高度為 auto 時,拉伸占滿容器。

flex-start:

flex-end:

center:

baseline(視覺上與 flex-start 類似):

stretch(默認):

alignContent:多根軸線的對齊方式

只在換行產生多行時生效,只有一根軸線時該屬性無作用。

  • flex-start:與交叉軸起點對齊。
  • flex-end:與交叉軸終點對齊。
  • center:與交叉軸中點對齊。
  • space-between:與交叉軸兩端對齊,軸線之間間隔均勻分布。
  • space-around:每根軸線兩側間隔相等。
  • stretch(默認):軸線拉伸占滿整個交叉軸。

二、子元素屬性

  • layout_order:控制排列順序。默認按視圖順序排列,數值越小越靠前,可為負值。
  • layout_flexGrow:剩餘空間的放大比例,默認 0(不放大);設為 1 時等分剩餘空間。
  • layout_flexShrink:空間不足時的縮小比例,默認 1。若多個元素都為 1,空間不足時等比縮小;負值無效。
  • layout_alignSelf:允許單個子元素覆蓋容器的 alignItems 設置,默認 auto(繼承父容器),若無父容器則等同 stretch。
  • layout_flexBasisPercent:在分配剩餘空間之前,該子元素佔據的主軸長度百分比,瀏覽器/佈局引擎依此判斷主軸是否還有多餘空間。默認 auto,即元素自身大小。

三、代碼示例

<com.google.android.flexbox.FlexboxLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/flexbox_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:alignContent="flex_start"
    app:alignItems="flex_start"
    app:flexWrap="wrap"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/textview1"
        style="@style/FlexItem"
        android:layout_width="@dimen/flex_item_length2"
        android:layout_height="@dimen/flex_item_length"
        android:text="@string/one"/>

    <TextView
        android:id="@+id/textview2"
        style="@style/FlexItem"
        android:layout_width="@dimen/flex_item_length3"
        android:layout_height="@dimen/flex_item_length"
        android:text="@string/two"/>

    <TextView
        android:id="@+id/textview3"
        style="@style/FlexItem"
        android:layout_width="@dimen/flex_item_length"
        android:layout_height="@dimen/flex_item_length"
        android:text="@string/three"/>
</com.google.android.flexbox.FlexboxLayout>

四、依賴與源碼

compile 'com.google.android:flexbox:0.1.2'

源碼已開放在 GitHub 上,感興趣可以直接下載研讀。總的來說,FlexboxLayout 最適合那種「需要自動換行的標籤流」「等分排列的卡片列表」場景,用它可以省掉大量嵌套與動態計算寬度的代碼。

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