flexbox-layout README

The project's README from github.com/google/flexbox-layout. As one markdown file: README.md.

FlexboxLayout

Circle CI

FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.

Installation

Add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
}

Starting from 3.0.0, the groupId is changed to com.google.android.flexbox in preparation to uploading the artifacts to google maven. You can still download the artifacts from jcenter for the past versions with the prior groupId (com.google.android), but migrating the library 3.0.0 is recommended.

Note that the default values for alignItems and alignContent for FlexboxLayout have been changed from stretch to flex_start starting from 2.0.0, it may break the existing apps. Please make sure to set stretch explicitly if you want to apply the behavior of stretch.

Note that starting from 1.1.0, the library is expeced to use with AndroidX. Please migrate to AndroidX if you use 1.1.0 or above.

Please use 1.0.0 if you haven't migrated to AndroidX.

Usage

There are two ways of using Flexbox in your layout.

FlexboxLayout

The first one is FlexboxLayout that extends the ViewGroup like LinearLayout and RelativeLayout. You can specify the attributes from a layout XML like:

<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:flexWrap="wrap"
    app:alignItems="stretch"
    app:alignContent="stretch" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="120dp"
        android:layout_height="80dp"
        app:layout_flexBasisPercent="50%"
        />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:layout_alignSelf="center"
        />

    <TextView
        android:id="@+id/textview3"
        android:layout_width="160dp"
        android:layout_height="80dp"
        app:layout_alignSelf="flex_end"
        />
</com.google.android.flexbox.FlexboxLayout>

Or from code like:

FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout);
flexboxLayout.setFlexDirection(FlexDirection.ROW);

View view = flexboxLayout.getChildAt(0);
FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams();
lp.setOrder(-1);
lp.setFlexGrow(2);
view.setLayoutParams(lp);

FlexboxLayoutManager (within RecyclerView)

The second one is FlexboxLayoutManager that can be used within RecyclerView.

RecyclerView recyclerView = (RecyclerView) context.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setJustifyContent(JustifyContent.FLEX_END);
recyclerView.setLayoutManager(layoutManager);

or for the attributes for the children of the FlexboxLayoutManager you can do like:

mImageView.setImageDrawable(drawable);
ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
    FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
    flexboxLp.setFlexGrow(1.0f);
    flexboxLp.setAlignSelf(AlignSelf.FLEX_END);
}

The advantage of using FlexboxLayoutManager is that it recycles the views that go off the screen for reuse for the views that are appearing as the user scrolls instead of inflating every individual view, which consumes much less memory especially when the number of items contained in the Flexbox container is large.

FlexboxLayoutManager in action

Supported attributes/features comparison

Due to some characteristics of RecyclerView, some Flexbox attributes are not available/not implemented to the FlexboxLayoutManager. Here is a quick overview of the attributes/features comparison between the two implementations.

Attribute / Feature FlexboxLayout FlexboxLayoutManager (RecyclerView)
flexDirection Check Check
flexWrap Check Check (except wrap_reverse)
justifyContent Check Check
alignItems Check Check
alignContent Check -
layout_order Check -
layout_flexGrow Check Check
layout_flexShrink Check Check
layout_alignSelf Check Check
layout_flexBasisPercent Check Check
layout_(min/max)Width Check Check
layout_(min/max)Height Check Check
layout_wrapBefore Check Check
Divider Check Check
View recycling - Check
Scrolling *1 Check

*1 Partially possible by wrapping it with ScrollView. But it isn't likely to work with a large set of views inside the layout. Because it doesn't consider view recycling.

Supported attributes

Attributes for the FlexboxLayout:

Attributes for the children of a FlexboxLayout

Others

Known differences from the original CSS specification

This library tries to achieve the same capabilities of the original Flexible Box specification as much as possible, but due to some reasons such as the way specifying attributes can't be the same between CSS and Android XML, there are some known differences from the original specification.

(1) There is no flex-flow equivalent attribute

(2) There is no flex equivalent attribute

(3) layout_flexBasisPercent is introduced instead of flexBasis

(4) layout_wrapBefore is introduced.

(5) Default values for alignItems and alignContent are set to flex_start instead of stretch.

Xamarin Binding

Xamarin binding is now available on NuGet thanks to @btripp

Demo apps

Flexbox Playground demo app

The demo-playground module works as a playground demo app for trying various values for the supported attributes. You can install it by

./gradlew demo-playground:installDebug

The demo-cat-gallery module showcases the usage of the FlexboxLayoutManager inside the RecyclerView that handles various sizes of views aligned nicely regardless of the device width like the Google Photo app without loading all the images on the memory. Thus compared to using the {@link FlexboxLayout}, it's much less likely to abuse the memory, which sometimes leads to the OutOfMemoryError.

./gradlew demo-cat-gallery:installDebug

How to make contributions

Please read and follow the steps in CONTRIBUTING.md

License

Please see LICENSE

flexbox-layout (google/flexbox-layout)

Searching Database…

18.3K ·GitHub
archived
这是什么?FlexboxLayout 是一个开源的 Android 库,把 CSS 弹性盒子布局模块(flexbox)的能力带到了 Android 应用布局中。
它是需要添加到项目里的库,还是一个独立的应用?它是一个通过 Gradle 依赖添加的库。核心是 FlexboxLayout,一个类似于 LinearLayout 或 RelativeLayout 的 ViewGroup,另外还有一个用于 RecyclerView 的 FlexboxLayoutManager。
怎么把它添加到 Android 项目里?把 flexbox 依赖添加到你的 build.gradle。从 3.0.0 开始,groupId 是 com.google.android.flexbox,构建包托管在 Google Maven 上;旧版本仍然可以从 jcenter 下载,使用之前的 groupId。
我需要先把项目迁移到 AndroidX 吗?新版本需要:从 1.1.0 开始,这个库要求 AndroidX。如果你的项目还没迁移,就锁定 1.0.0 版本。
升级时有什么要注意的坑?在 2.0.0 中,alignItems 和 alignContent 的默认值从 stretch 改成了 flexstart,这可能会悄悄改变现有布局的显示效果——如果你依赖 stretch 的行为,请显式地设置它。
它只能用于原生 Android,还是也能用在其他地方?它是为原生 Android 应用开发的。针对基于 Xamarin 的移动项目,NuGet 上也提供了 Xamarin 绑定。
在布局中使用 flexbox 有哪两种方式?一种是 FlexboxLayout,一个普通的 ViewGroup,可以通过布局 XML 或代码来配置;另一种是 FlexboxLayoutManager,在需要可滚动的 flex 项目列表时交给 RecyclerView 使用。
和普通的 FlexboxLayout 相比,为什么选择 FlexboxLayoutManager?它会回收滚出屏幕的视图,而不是把每个子项都创建出来,所以当容器里有很多条目时内存占用也能保持在较低水平。代价是:wrapreverse、alignContent 和分隔线等属性在那里不受支持,而且不使用回收机制滚动一个大布局也不现实。
它和 CSS flexbox 规范的一致程度如何?相当接近,但有明确记录的差异:没有 flex-flow 或 flex 这类简写属性,layoutflexBasisPercent 只接受百分比而不是 flex-basis 值,另外多了一个 layoutwrapBefore 属性用于强制换行。默认值也不同——是 flexstart 而不是 stretch。
这个仓库还在积极维护吗?不——GitHub 上 google/flexbox-layout 已被归档,代码是只读的,不再开发。已发布的构建包在 Google Maven 上仍然可以获取。
Creator
google
Size
48.2 MB
Stars
18.3K
Age
10y
Commits · 90 Days