下载:3.0.03.0.0 ↓3.0.02021-05-21
+0
日增长
18,272
stars
1,795
forks
0
watchers
292
prs
336
issues
25
releases

README

FlexboxLayout
 ![Circle CI ](https://circleci.com/gh/google/flexbox-layout/tree/main)

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:
xml


    

    

    



Or from code like:
java
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.

java
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:

java
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 Re

Top issues

Latest release

3.0.0 3.0.0 2021-05-21
Artifacts are now hosted on google maven

Starting from 3.0.0 and moving forward, this library is hosted on google maven.

In relation to the change of the hosted server, the groupId of this library was changed from com.google.android to com.google.android.flexbox. Old versions of libraries are still hosted on JCenter as read only mode.

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON