FlexboxLayout is a library project which brings the similar capabilities ofCSS Flexible Box Layout Module to Android.
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.
There are two ways of using Flexbox in your layout.
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);
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 screenfor 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.
Due to some characteristics of RecyclerView
, some Flexbox attributes are not available/not implementedto the FlexboxLayoutManager
.Here is a quick overview of the attributes/features comparison between the two implementations.
*1 Partially possible by wrapping it with ScrollView
. But it isn't likely to work with a large setof views inside the layout. Because it doesn't consider view recycling.
flexDirection
flexWrap
justifyContent
alignItems
alignContent
showDividerHorizontal (one or more of none | beginning | middle | end
)
dividerDrawableHorizontal (reference to a drawable)
column
or column_rebase
).showDividerVertical (one or more of none | beginning | middle | end
)
dividerDrawableVertical (reference to a drawable)
column
or column_rebase
).showDivider (one or more of none | beginning | middle | end
)
dividerDrawable (reference to a drawable)
justifyContent="space_around"
or alignContent="space_between"
... etc) for puttingspaces between flex lines or flex items, you may see unexpected spaces. Please avoid using theseat the same time.Example of putting both vertical and horizontal dividers.
res/drawable/divider.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
<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" />
layout_order (integer)
layout_flexGrow (float)
This attribute determines how much this child will grow if positive free space isdistributed relative to the rest of other flex items included in the same flex line.If a flex item has a positive layout_flexGrow
value, the item will take up the remainingspace in the flex line. If multiple flex items in the same flex line have positive layout_flexGrow
values, the remaining free space is distributed depending on the proportion of their declaredlayout_flexGrow
value. (Similar to the layout_weight
attribute in the LinearLayout
)If not specified, 0
is set as a default value.
layout_flexShrink (float)
layout_alignSelf
This attribute determines the alignment along the cross axis (perpendicular to themain axis). The alignment in the same direction can be determined by thealignItems
in the parent, but if this is set to other thanauto
, the cross axis alignment is overridden for this child. Possible values are:
layout_flexBasisPercent (fraction)
The initial flex item length in a fraction format relative to its parent.The initial main size of this child view is trying to be expanded as the specifiedfraction 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 isMeasureSpec.EXACTLY
). The default value is -1
, which means not set.
layout_minWidth / layout_minHeight (dimension)
These attributes impose minimum size constraints for the children of FlexboxLayout.A child view won't shrink less than the value of these attributes (varies based on theflexDirection
attribute as to which attribute imposes the size constraint along themain axis) regardless of the layout_flexShrink
attribute.
layout_maxWidth / layout_maxHeight (dimension)
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 theflexDirection
attribute as to which attribute imposes the size constraint along themain axis) regardless of the layout_flexGrow
attribute.
layout_wrapBefore (boolean)
This attribute forces a flex line wrapping, the default value is false
.i.e. if this is set to true
for aflex item, the item will become the first item of a flex line. (A wrapping happensregardless of the flex items being processed in the previous flex line)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 Modulespecification, but having this attribute is useful for Android developers. For example, to flattenthe layouts when building a grid-like layout or for a situation where developers wantto put a new flex line to make a semantic difference from the previous one, etc.
This library tries to achieve the same capabilities of the originalFlexible Box specification as much as possible,but due to some reasons such as the way specifying attributes can't be the same betweenCSS and Android XML, there are some known differences from the original specification.
(1) There is no flex-flowequivalent attribute
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 equivalent attribute
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 offlexBasis
layout_flexBasisPercent
in this library and flex-basis
property in the CSS are used todetermine the initial length of an individual flex item. The flex-basis
property accepts widthvalues such as 1em
, 10px
, and content
as strings as well as percentage values such as10%
and 30%
. layout_flexBasisPercent
only accepts percentage values.However, specifying initial fixed width values can be done by specifying width (or height) values inlayout_width (or layout_height, varies depending on the flexDirection
). Also, the sameeffect can be done by specifying "wrap_content" in layout_width (or layout_height) ifdevelopers want to achieve the same effect as 'content'. Thus, layout_flexBasisPercent
onlyaccepts percentage values, which can't be done through layout_width (or layout_height) forsimplicity.(4) layout_wrapBefore
is introduced.
(5) Default values for alignItems
and alignContent
are set to flex_start
instead of stretch
.
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.Xamarin binding is now available on NuGet thanks to @btripp
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 RecyclerViewthat handles various sizes of views aligned nicely regardless of the device width like theGoogle 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
Please read and follow the steps in CONTRIBUTING.md
Please see LICENSE
FlexBox 是一个 jQuery 插件用来显示动态下拉框,下拉数据可直接进行翻页显示,使用的是 JSON 格式的数据源。 It can be used as a: ComboBox, with per-result html templates Suggest box, like Google's search Data-driven type-ahead input box It suppo
问题内容: 昨天我在学校里有一个使用CSS 3 flexbox语句的网站。我以前从未用过。因此,我用Google搜索了一下,发现了很多不同样式的flexbox语句。 有些人写,有些用,还有一些。 那有什么区别呢?我应该使用哪个? 问题答案: 这些是不同的风格。 是2009年 的版本。 是2011年的版本。是实际版本。 保罗·爱尔兰的名言 警告:Flexbox进行了一些重大修订,因此本文已过时。总而
Flexbox CSS Css3-flexbox Flexbox——快速布局神器 一个完整的Flexbox指南 CSS3 Flexbox属性可视化指南 Autoprefixer 布局问题 Flexbox属性图 从第一个子节点可以看到Flexbox由Flex容器和Flex项目组成,容器即父元素,项目即子元素。他们之间的一些关系可以这样来表示: Flex容器 容器默认存在两根轴:水平的主轴(main
React-Flexbox-Grid 是实现了 flexboxgrid.css 效果的 React 组件集合,基于一些 CSS 模块(使用 SASS 编写),Webpack 和 ES6。 使用: import React from `react`;import {Container} from `react-flexbox-grid/lib/index`;React.render(<Contain
问题内容: 任何人都可以请我 启发 一下新的Flexbox布局模型比当前表格更好的方法吗?( display:table和所有这些都包含在我的案例中 )? IE10根本不支持它,这在不久的将来不是很好,我只是看不到表格布局有任何好处。但是,尽管如此,互联网仍然开始充满这种新的CSS布局方法的“崇拜者”,而且我看到的所有示例都可以使用普通的CSS轻松完成而不会出现问题。 15年12月25日更新: 自
Bootstrap 终于支持了弹性盒。只需用少量变量或者交换一下样式表就可以使用这个崭新的CSS布局样式。 它包含了什么? 在一系列Bootstrap的组件中,弹性盒支持都是可用的。 整个网格系统(混合和预定义的类),float变换成display: flex;。 输入框组,由display: table;变成display: flex; 。 媒体组件display: table;和一系列hack