Posted by Takeshi Hagikura, Developer Programs Engineer
At Google I/O last year we announced .
At the same time, we open sourced to Android. Here are some cases where
FlexboxLayout is particularly effective.FlexboxLayout can be interpreted as an advanced LinearLayoutbecause both layouts align their child views sequentially. The significant
difference between LinearLayout and
FlexboxLayout is thatFlexboxLayout has a feature for wrapping.That means if you add the flexWrap="wrap" attribute,
FlexboxLayout puts a view to a new line if there is not enoughspace left in the current line as shown in the picture below.
Nexus5X portrait
Pixel C with multi window mode enabled, divider line on the left.
Pixel C with multi window mode enabled, divider line on the right.
You would need to define multiple DP-bucket layouts (such as layout-600dp,
layout-720dp, layout-1020dp) to handle various screen sizes with traditional
layouts such as
LinearLayout or RelativeLayout. Butthe dialog above is built with a single
FlexboxLayout.The technique used in the example is setting the
flexWrap="wrap" asexplained above,
<com .google.android.flexbox.flexboxlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexwrap="wrap">
then you can get the following layout where child views are aligned to a new
line instead of overflowing its parent.
You can check out the complete .
With the latest release of the , in the
FlexboxLayout repository. As you can see inthe repository, each image shown in
RecyclerView has a differentwidth. But by setting the flexWrap setting to wrap,
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
layoutManager.setFlexWrap(FlexWrap.WRAP);
and setting the
flexGrow (as you can see, you can configure theattributes through
FlexboxLayoutManager andFlexboxLayoutManager.LayoutParams for child attributes instead ofconfiguring it from xml) attribute to a positive value for each child,
void bindTo(Drawable drawable) {
mImageView.setImageDrawable(drawable);
ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp =
(FlexboxLayoutManager.LayoutParams) mImageView.getLayoutParams();
flexboxLp.setFlexGrow(1.0f);
}
}you can see every image fits within the layout nicely regardless of the screen
orientation.
- using
FlexboxLayout andFlexboxLayoutManager.attributes to build flexible layouts tailored for your needs. We're very open to
hear your feedback, if you find any issues or feature requests, please file an
SOCIAL SHARE CARD GENERATOR