<Android 基础(四十)> FlexboxLayout

来源:互联网 发布:linux sslocal 编辑:程序博客网 时间:2024/05/18 19:46

转载于:http://blog.csdn.net/poorkick/article/details/77394663

3.5 alignContent

alignContent , 表示控件在副轴上的对齐方向(针对多行元素)。支持flex_start , flex_end , center , space_between , space_around , stretch六种取值

flexboxlayout改成match_parent , 以flexDirection为row , flexWrap为wrap为例, 看看效果

alignContent效果图flex_start这里写图片描述flex_end这里写图片描述center这里写图片描述space_between这里写图片描述space_around这里写图片描述stretch这里写图片描述

3.6 showDivider and dividerDrawable

showDivider , 显示分割线。支持none , beginning , middle , end 四种取值

绘制一条分割线,然后flexDirection为row , flexWrap为wrap,dividerDrawable为@drawable/divider为例,看看效果

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="@color/colorAccent"></solid>    <size android:width="2dp"/></shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
showDivider效果图none这里写图片描述beginning这里写图片描述middle这里写图片描述end这里写图片描述

3.7 showDividerHorizontal and dividerDrawableHorizontal

横向分割线,支持none , beginning , middle , end 四种取值,四种取值和上面的使用差不多,我们只举一个例子

分割线

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="@color/colorAccent"></solid>    <size  android:height="2dp"/></shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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">    <com.google.android.flexbox.FlexboxLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#EBEBEB"        app:flexDirection="column"        app:showDividerHorizontal="middle"        app:dividerDrawableHorizontal="@drawable/divider"        app:flexWrap="wrap">        <TextView            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:text="Android" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="android studio" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="Android安全" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="Android框架" />    </com.google.android.flexbox.FlexboxLayout></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 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

这里写图片描述

3.8 showDividerVertical and dividerDrawableVertical

和上面的用法相似,看下效果把

分割线

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="@color/colorAccent"></solid>    <size  android:width="2dp"/></shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

这里写图片描述


4. FlexboxLayout子元素属性自测

4.1 layout_order

默认情况

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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">    <com.google.android.flexbox.FlexboxLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#EBEBEB"        app:flexDirection="row"        app:flexWrap="wrap">        <TextView            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:gravity="center"            android:text="Android" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="android studio" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="Android安全" />    </com.google.android.flexbox.FlexboxLayout></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

这里写图片描述

顺序排一下 2,3,1

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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">    <com.google.android.flexbox.FlexboxLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#EBEBEB"        app:flexDirection="row"        app:flexWrap="wrap">        <TextView            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            app:layout_order="2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:gravity="center"            android:text="Android" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            app:layout_order="3"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="android studio" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            app:layout_order="1"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="Android安全" />     </com.google.android.flexbox.FlexboxLayout></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

这里写图片描述

4.2 layout_flexGrow

layout_flexGrow ,子元素的放大比例, 决定如何分配剩余空间(如果存在剩余空间的话),默认值为0,不会分配剩余空间,如果某一行中有一个item的 layout_flexGrow 是一个正值,那么会将这行全部剩余空间分配给这个Item , 如果这一行有多个Item这个属性都为正值,那么剩余空间的按照layout_flexGrow定义的比例(有点像LinearLayout的layout_weight属性)进行分配

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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">    <com.google.android.flexbox.FlexboxLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#EBEBEB"        app:flexDirection="row"        app:flexWrap="wrap">        <TextView            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            app:layout_flexGrow="10.0"            android:text="Android" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            android:text="android studio" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:background="@drawable/item_bg"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:paddingTop="4dp"            android:paddingBottom="4dp"            app:layout_flexGrow="10.0"            android:text="Android安全" />     </com.google.android.flexbox.FlexboxLayout></RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

这里写图片描述 
可以看到黄色框内的两个元素(也就是第一个子元素和第三个子元素)按照1:1 的比例分配了这一行中剩余的空间。

4.3 layout_flexShrink

layout_flexShrink , 子元素缩小比例 , 与上面的属性值有点对应关系,当空间不足时,子元素需要缩小(针对单行),默认值为1,如果所有子元素的layout_flexShrink 值为1,空间不足时,都等比缩小,如果有一个为0,其他为1,空间不足时,为0的不缩小,负值无效。

这里引用Github上的一个图,看看效果 
缩小比例

4.4 layout_alignSelf

layout_alignSelf 属性可以给子元素设置对齐方式,上面讲的alignItems属性可以设置对齐,这个属性的功能和alignItems一样,只不过alignItems作用于所有子元素,而layout_alignSelf 作用于单个子元素。默认值为auto, 表示继承alignItems属性,如果为auto以外的值,则会覆盖alignItems属性。支持auto , flex_start , flex_end , center , baseline , stretch六种取值。除了auto以外,其他和alignItems属性一样,而auto表示继承alignItems的属性,所以基本上效果相同,这里不做过多解释。

4.5 layout_flexBasisPercent

layout_flexBasisPercen , 表示设置子元素的长度为它父容器长度的百分比。这个值只有设置了父容器的长度时才有效(也就是MeasureSpec mode 是 MeasureSpec.EXACTLY)。默认值时-1。

设置第一个元素 
app:layout_flexBasisPercent=”50%”

这里写图片描述

4.6 layout_minWidth / layout_minHeight

强制限制 FlexboxLayout的子元素(宽或高)不会小于最小值,不管layout_flexShrink这个属性的值为多少,子元素不会被缩小到小于设置的这个最小值。

4.7 layout_maxWidth / layout_maxHeight

这个和上面的刚好相反,强制限制FlexboxLayout子元素不会大于这个最大值, 不管layout_flexGrow的值为多少,子元素不会被放大到超过这个最大值。

4.8 layout_wrapBefore

layout_wrapBefore 属性控制强制换行,默认值为false,如果将一个子元素的这个属性设置为true,那么这个子元素将会成为一行的第一个元素。这个属性将忽略flex_wrap 设置的 noWrap值。

设置第三个元素 
app:layout_wrapBefore=”true”

这里写图片描述

可以看到第三个元素另起了一行