README.md 17.5 KB
Newer Older
T
Takeshi Hagikura 已提交
1
# FlexboxLayout
2 3
[ ![Circle CI](https://circleci.com/gh/google/flexbox-layout.svg?style=shield&circle-token=2a42716dfffab73d73c5ce7ed7b3ee620cfa137b) ](https://circleci.com/gh/google/flexbox-layout/tree/master)
[ ![Download](https://api.bintray.com/packages/google/flexbox-layout/flexbox/images/download.svg) ](https://bintray.com/google/flexbox-layout/flexbox/_latestVersion)
4

T
Takeshi Hagikura 已提交
5 6 7
FlexboxLayout is a library project which brings the similar capabilities of
[CSS Flexible Box Layout Module](https://www.w3.org/TR/css-flexbox-1) to Android.

8
# Installation
S
Scott Radvan 已提交
9
Add the following dependency to your `build.gradle` file:
T
Takeshi Hagikura 已提交
10 11 12

```
dependencies {
13
    implementation 'com.google.android:flexbox:2.0.1'
14
}
T
Takeshi Hagikura 已提交
15 16
```

T
Takeshi Hagikura 已提交
17 18 19 20
**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`.**


T
Takeshi Hagikura 已提交
21
**Note that starting from 1.1.0, the library is expected to use with AndroidX. Please migrate to [AndroidX](https://developer.android.com/jetpack/androidx/migrate) if you use 1.1.0 or above.**
T
Takeshi Hagikura 已提交
22

T
Takeshi Hagikura 已提交
23
**Please use 1.0.0 if you haven't migrated to AndroidX.**
T
Takeshi Hagikura 已提交
24 25


26
# Usage
27
There are two ways of using Flexbox in your layout.
28

29
## FlexboxLayout
30
The first one is `FlexboxLayout` that extends the `ViewGroup` like `LinearLayout` and `RelativeLayout`.
T
Takeshi Hagikura 已提交
31 32
You can specify the attributes from a layout XML like:
```xml
33
<com.google.android.flexbox.FlexboxLayout
T
Takeshi Hagikura 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    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"
        />
62
</com.google.android.flexbox.FlexboxLayout>
T
Takeshi Hagikura 已提交
63 64 65 66 67
```

Or from code like:
```java
FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout);
68
flexboxLayout.setFlexDirection(FlexDirection.ROW);
T
Takeshi Hagikura 已提交
69 70 71 72 73 74 75 76

View view = flexboxLayout.getChildAt(0);
FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams();
lp.order = -1;
lp.flexGrow = 2;
view.setLayoutParams(lp);
```

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
## 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);
}
```
T
Takeshi Hagikura 已提交
99

100
The advantage of using `FlexboxLayoutManager` is that it recycles the views that go off the screen
T
Takeshi Hagikura 已提交
101 102 103 104 105
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](/assets/flexbox-layoutmanager.gif)

106

R
Rajat Kumar Gupta 已提交
107
## Supported attributes/features comparison
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|flexWrap|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png) (except `wrap_reverse`)|
|justifyContent|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|alignItems|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|alignContent|![Check](/assets/pngs/check_green_small.png)| - |
|layout_order|![Check](/assets/pngs/check_green_small.png)| - |
|layout_flexGrow|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_flexShrink|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_alignSelf|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_flexBasisPercent|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_(min/max)Width|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_(min/max)Height|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|layout_wrapBefore|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|Divider|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)|
|View recycling| - |![Check](/assets/pngs/check_green_small.png)|
|Scrolling| *1 |![Check](/assets/pngs/check_green_small.png)|

R
Rajat Kumar Gupta 已提交
131
*1 Partially possible by wrapping it with `ScrollView`. But it isn't likely to work with a large set
132 133 134 135 136
   of views inside the layout. Because it doesn't consider view recycling.

# Supported attributes

## Attributes for the FlexboxLayout:
T
Takeshi Hagikura 已提交
137

138
* __flexDirection__
T
Takeshi Hagikura 已提交
139
  * This attribute determines the direction of the main axis (and the cross axis, perpendicular to the main axis). The direction children items are placed inside the Flexbox layout.
T
Takeshi Hagikura 已提交
140 141 142 143 144 145 146 147
  Possible values are:
    * row (default)
    * row_reverse
    * column
    * column_reverse

    ![Flex Direction explanation](/assets/flex-direction.gif)

148
* __flexWrap__
T
Takeshi Hagikura 已提交
149
  * This attribute controls whether the flex container is single-line or multi-line, and the
E
Eddie Ringle 已提交
150
  direction of the cross axis. Possible values are:
T
Takeshi Hagikura 已提交
151 152 153 154 155 156
    * nowrap (default)
    * wrap
    * wrap_reverse

    ![Flex Wrap explanation](/assets/flex-wrap.gif)

157
* __justifyContent__
T
Takeshi Hagikura 已提交
158 159 160 161 162 163 164 165 166
  * This attribute controls the alignment along the main axis. Possible values are:
    * flex_start (default)
    * flex_end
    * center
    * space_between
    * space_around

    ![Justify Content explanation](/assets/justify-content.gif)

167
* __alignItems__
T
Takeshi Hagikura 已提交
168
  * This attribute controls the alignment along the cross axis. Possible values are:
T
Takeshi Hagikura 已提交
169
    * flex_start (default)
T
Takeshi Hagikura 已提交
170 171 172
    * flex_end
    * center
    * baseline
T
Takeshi Hagikura 已提交
173
    * stretch
T
Takeshi Hagikura 已提交
174 175 176

    ![Align Items explanation](/assets/align-items.gif)

177
* __alignContent__
T
Takeshi Hagikura 已提交
178 179
  * This attribute controls the alignment of the flex lines in the flex container. Possible values
  are:
T
Takeshi Hagikura 已提交
180
    * flex_start (default)
T
Takeshi Hagikura 已提交
181 182 183 184
    * flex_end
    * center
    * space_between
    * space_around
T
Takeshi Hagikura 已提交
185
    * stretch
T
Takeshi Hagikura 已提交
186 187 188

    ![Align Content explanation](/assets/align-content.gif)

189 190
* __showDividerHorizontal__ (one or more of `none | beginning | middle | end`)
* __dividerDrawableHorizontal__ (reference to a drawable)
191 192 193
  * Puts horizontal dividers between flex lines (or flex items when flexDirection
  is set to `column` or `column_rebase`).
  
194 195
* __showDividerVertical__ (one or more of `none | beginning | middle | end`)
* __dividerDrawableVertical__ (reference to a drawable)
196 197 198
  * Puts vertical dividers between flex items (or flex lines when flexDirection
  is set to `column` or `column_rebase`).

199 200
* __showDivider__ (one or more of `none | beginning | middle | end`)
* __dividerDrawable__ (reference to a drawable)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  * Shorthand for setting both horizontal and vertical dividers. Note that if used with other attributes
  (such as `justifyContent="space_around"` or `alignContent="space_between"` ... etc) for putting 
  spaces between flex lines or flex items, you may see unexpected spaces. Please avoid using these
  at the same time.
  
  Example of putting both vertical and horizontal dividers.
  
  `res/drawable/divider.xml`
  ```xml
  <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size
        android:width="8dp"
        android:height="12dp" />
    <solid android:color="#44A444" />
  </shape> 
  ```
  
  `res/layout/content_main.xml`
  ```xml
  <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:alignContent="flex_start"
    app:alignItems="flex_start"
    app:flexWrap="wrap"
    app:showDivider="beginning|middle"
    app:dividerDrawable="@drawable/divider" >

    <TextView
        style="@style/FlexItem"
        android:layout_width="220dp"
        android:layout_height="80dp"
        android:text="1" />
    <TextView
        style="@style/FlexItem"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:text="2" />
    <TextView
        style="@style/FlexItem"
        android:layout_width="160dp"
        android:layout_height="80dp"
        android:text="3" />
    <TextView
        style="@style/FlexItem"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:text="4" />
    <TextView
        style="@style/FlexItem"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:text="5" />
  ```
  
  ![Dividers beginning and middle](/assets/divider-beginning-middle.png)

T
Takeshi Hagikura 已提交
259

260
## Attributes for the children of a FlexboxLayout
T
Takeshi Hagikura 已提交
261

262
* __layout_order__ (integer)
S
Scott Radvan 已提交
263
  * This attribute can change how the ordering of the children views are laid out.
T
Takeshi Hagikura 已提交
264 265 266
  By default, children are displayed and laid out in the same order as they appear in the
  layout XML. If not specified, `1` is set as a default value.

267 268
    ![Order explanation](/assets/layout_order.gif)

269
* __layout_flexGrow__ (float)
T
Takeshi Hagikura 已提交
270 271
  * This attribute determines how much this child will grow if positive free space is
  distributed relative to the rest of other flex items included in the same flex line.
272 273
  If a flex item has a positive `layout_flexGrow` value, the item will take up the remaining
  space in the flex line. If multiple flex items in the same flex line have positive `layout_flexGrow`
H
hotchemi 已提交
274
  values, the remaining free space is distributed depending on the proportion of their declared
275
  `layout_flexGrow` value. (Similar to the `layout_weight` attribute in the `LinearLayout`)
T
Takeshi Hagikura 已提交
276 277
  If not specified, `0` is set as a default value.

278 279
    ![Flex Grow explanation](/assets/layout_flexGrow.gif)

280
* __layout_flexShrink__ (float)
T
Takeshi Hagikura 已提交
281
  * This attribute determines how much this child will shrink if negative free space is
T
Takeshi Hagikura 已提交
282 283 284
  distributed relative to the rest of other flex items included in the same flex line.
  If not specified, `1` is set as a default value.

285 286
    ![Flex Shrink explanation](/assets/layout_flexShrink.gif)

287
* __layout_alignSelf__
E
Eddie Ringle 已提交
288
  * This attribute determines the alignment along the cross axis (perpendicular to the
T
Takeshi Hagikura 已提交
289 290 291 292 293 294 295 296 297 298
  main axis). The alignment in the same direction can be determined by the
  `alignItems` in the parent, but if this is set to other than
  `auto`, the cross axis alignment is overridden for this child. Possible values are:
    * auto (default)
    * flex_start
    * flex_end
    * center
    * baseline
    * stretch

299 300
    ![Align Self explanation](/assets/layout_alignSelf.gif)

301
* __layout_flexBasisPercent__ (fraction)
T
Takeshi Hagikura 已提交
302
  * The initial flex item length in a fraction format relative to its parent.
S
Scott Radvan 已提交
303
  The initial main size of this child view is trying to be expanded as the specified
T
Takeshi Hagikura 已提交
304 305 306 307 308 309
  fraction against the parent main size.
  If this value is set, the length specified from `layout_width`
  (or `layout_height`) is overridden by the calculated value from this attribute.
  This attribute is only effective when the parent's length is definite (MeasureSpec mode is
  `MeasureSpec.EXACTLY`). The default value is `-1`, which means not set.

310 311
    ![Flex basis percent explanation](/assets/layout_flexBasisPercent.gif)

312
* __layout_minWidth__ / __layout_minHeight__ (dimension)
T
Takeshi Hagikura 已提交
313
  * These attributes impose minimum size constraints for the children of FlexboxLayout.
R
Rajat Kumar Gupta 已提交
314
  A child view won't shrink less than the value of these attributes (varies based on the
T
Takeshi Hagikura 已提交
315 316 317
  `flexDirection` attribute as to which attribute imposes the size constraint along the
  main axis) regardless of the `layout_flexShrink` attribute.

318 319
    ![Min width explanation](/assets/layout_minWidth.gif)

320
* __layout_maxWidth__ / __layout_maxHeight__ (dimension)
T
Takeshi Hagikura 已提交
321 322 323 324 325
  * These attributes impose maximum size constraints for the children of FlexboxLayout.
  A child view won't be expanded more than the value of these attributes (varies based on the
  `flexDirection` attribute as to which attribute imposes the size constraint along the
  main axis) regardless of the `layout_flexGrow` attribute.

326 327
    ![Max width explanation](/assets/layout_maxWidth.gif)

328
* __layout_wrapBefore__ (boolean)
329
  * This attribute forces a flex line wrapping, the default value is `false`.
T
Takeshi Hagikura 已提交
330
  i.e. if this is set to `true` for a
331
  flex item, the item will become the first item of a flex line. (A wrapping happens
R
Rajat Kumar Gupta 已提交
332
  regardless of the flex items being processed in the previous flex line)
333 334
  This attribute is ignored if the `flex_wrap` attribute is set to `nowrap`.
  The equivalent attribute isn't defined in the original CSS Flexible Box Module
T
Takeshi Hagikura 已提交
335
  specification, but having this attribute is useful for Android developers. For example, to flatten
R
Rajat Kumar Gupta 已提交
336
  the layouts when building a grid-like layout or for a situation where developers want
337 338
  to put a new flex line to make a semantic difference from the previous one, etc.

339 340
    ![Wrap before explanation](/assets/layout_wrapBefore.gif)

341 342
# Others

T
Takeshi Hagikura 已提交
343 344
## Known differences from the original CSS specification
This library tries to achieve the same capabilities of the original
T
Takeshi Hagikura 已提交
345
[Flexible Box specification](https://www.w3.org/TR/css-flexbox-1) as much as possible,
T
Takeshi Hagikura 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
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](https://www.w3.org/TR/css-flexbox-1/#flex-flow-property)
equivalent attribute
  * Because `flex-flow` is a shorthand for setting the `flex-direction` and `flex-wrap` properties,
  specifying two attributes from a single attribute is not practical in Android.

(2) There is no [flex](https://www.w3.org/TR/css-flexbox-1/#flex-property) equivalent attribute
  * Likewise `flex` is a shorthand for setting the `flex-grow`, `flex-shrink` and `flex-basis`,
  specifying those attributes from a single attribute is not practical.

(3) `layout_flexBasisPercent` is introduced instead of
  [flexBasis](https://www.w3.org/TR/css-flexbox-1/#flex-basis-property)
  * Both `layout_flexBasisPercent` in this library and `flex-basis` property in the CSS are used to
  determine the initial length of an individual flex item. The `flex-basis` property accepts width
E
Eddie Ringle 已提交
362
  values such as `1em`, `10px`, and `content` as strings as well as percentage values such as
T
Takeshi Hagikura 已提交
363 364
  `10%` and `30%`. `layout_flexBasisPercent` only accepts percentage values.
  However, specifying initial fixed width values can be done by specifying width (or height) values in
E
Eddie Ringle 已提交
365 366 367
  layout_width (or layout_height, varies depending on the `flexDirection`). Also, the same
  effect can be done by specifying "wrap_content" in layout_width (or layout_height) if
  developers want to achieve the same effect as 'content'. Thus, `layout_flexBasisPercent` only
T
Takeshi Hagikura 已提交
368 369 370
  accepts percentage values, which can't be done through layout_width (or layout_height) for
  simplicity.

371
(4) `layout_wrapBefore` is introduced.
H
hotchemi 已提交
372
  * The equivalent attribute doesn't exist in the CSS Flexible Box Module specification,
373 374 375
  but as explained above, Android developers will benefit by having this attribute for having
  more control over when a wrapping happens.

T
Takeshi Hagikura 已提交
376
(5) Default values for `alignItems` and `alignContent` are set to `flex_start` instead of `stretch`.
T
Takeshi Hagikura 已提交
377
  * Setting `stretch` for the `alignItems` is expensive because the children of `FlexboxLayout` are measured more than twice. The difference is more obvious when the layout hierarchy is deeply nested.
T
Takeshi Hagikura 已提交
378

379 380 381
## Xamarin Binding
Xamarin binding is now available on [NuGet](https://www.nuget.org/packages/FlexboxLayoutXamarinBindingAndroid/) thanks to [@btripp](https://github.com/btripp)

382 383
## Demo apps
### Flexbox Playground demo app
T
Takeshi Hagikura 已提交
384
The `demo-playground` module works as a playground demo app for trying various values for the supported attributes.
385 386
You can install it by
```
387
./gradlew demo-playground:installDebug
388 389
```

390
### Cat gallery demo app
391 392 393 394
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,
R
Rajat Kumar Gupta 已提交
395
which sometimes leads to the OutOfMemoryError.
396 397
```
./gradlew demo-cat-gallery:installDebug
398 399
```

T
Takeshi Hagikura 已提交
400
## How to make contributions
401
Please read and follow the steps in [CONTRIBUTING.md](/CONTRIBUTING.md)
T
Takeshi Hagikura 已提交
402 403

## License
404
Please see [LICENSE](/LICENSE)