'Android FlexboxLayoutManager last row alignment
I am trying to use FlexboxLayoutManager to layout square shape items. However the last row has only two items so that they centre align. That makes things look super bad. My code is below. Is there a way to fix this?
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN);
Solution 1:[1]
This is dirty, but works. Place the FlexLayout inside a FramLayout, Just don't forget to set the layout parameters to WRAP_CONTENT with center gravity over horizontal axis.
flexbox=new FlexboxLayout(context);
flexbox.setFlexDirection(FlexDirection.ROW);
flexbox.setFlexWrap(FlexWrap.WRAP);
flexbox.setJustifyContent(JustifyContent.FLEX_START);
FrameLayout container=new FrameLayout(context);
LayoutParams lp=new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
lp.gravity= Gravity.CENTER_HORIZONTAL;
container.addView(flexbox,lp);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ucMedia |

