参考资料: https://blog.stylingandroid.com/flexboxlayout-part2/
github项目地址: https://github.com/google/flexbox-layout
dependencies {
compile 'com.google.android:flexbox:0.2.2'
}
flexlayout
布局中,子视图按照xml书写,或属性layout_order
的数值顺序。沿主轴main axis
方向,即属性flexDirection
表示的方向排列,
如果当前主轴方向上累计的子视图总长度超过了布局在主轴方向上的长度时,子视图则向副轴cross axis
方向(垂直于主轴)上移动一段距离来另起一行放置。
flexlayout
很像流式布局,不过它的真正强大之处是可以按照百分比来设置视图大小和方便的设置视图间的等分间距。在某种程度上可以替代gridlayout
和tablelayout
。
定义了主轴的方向,类似于LinearLayout
的orientation
。
- row(default)
- row_reverse
- column
- column_reverse
定义了布局是否单行,和副轴的方向。
- nowrap(default)
- warp
- wrap_reverse
子视图在主轴方向上的对齐方式
- flex_start (default) 主轴同方向对齐
- flex_end 主轴逆方向对齐
- center 居中
- space_between 分散对齐,边缘子布局贴边,子布局间距相等
- space_around 分散对齐,边缘子布局不贴边,子布局间距和与布局的间距相等
子视图在副轴方向上的对齐方式,类似于LinearLayout
中的gravity
- stretch (default)
- flex_start
- flex_end
- center
- baseline
管理布局中多行之间的对齐方式
- stretch (default)
- flex_start
- flex_end
- center
- space_between
- space_around
用其手动指定子控件摆放的顺序,默认值为1
如果子控件在主轴方向上的总长度小于父布局的长度时,决定剩余空间的占有比例. 通常使用于多行布局(warp
,wrap_reverse
)中.
注意:不会触发父布局的重绘.假设主轴方向为row
,且子控件高度由宽度决定的话,有可能会出现副轴方向上控件的相互重叠情况
如果子控件在主轴方向上排满时,决定各控件的占有比例. 通常使用于单行布局(nowarp
)
主轴上一行由这一行中在副轴方向上最高的子控件决定,那么其他高度不足的控件在副轴方向上的排列方式就由属性layout_alignSelf
决定
- auto (default)
- flex_start
- flex_end
- center
- baseline
- stretch (拉伸)
指定控件在主轴方向上的长度较父布局的比例. 这一设置只有在父布局在这一方向上的测量参数(MeasureSpec)为MeasureSpec.EXACTLY
才能生效.
注意:在flexlayout的父布局是类似ScrollView的时候,纵向上使用这个属性是没有效果的.
控件的最小宽度/高度,layout_flexShrink
生效时,不会使控件大小小于设定的值
控件的最大宽度/高度,layout_flexGrow
生效时,不会使控件大小大于设定的值
是否让当前控件另起一行