xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“com.example.qyhl2.flexboxlayoutdemo.MainActivity”>
<com.google.android.flexbox.FlexboxLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:id=“@+id/flexboxLayout”
android:layout_width=“0dp”
android:layout_height=“0dp”
android:layout_marginBottom=“8dp”
android:layout_marginLeft=“8dp”
android:layout_marginRight=“8dp”
android:layout_marginTop=“8dp”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintHorizontal_bias=“0.5”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/button”
app:layout_constraintVertical_bias=“0.5”>
<TextView
android:id=“@+id/textview1”
android:layout_width=“120dp”
android:layout_height=“20dp”
android:layout_margin=“2dp”
android:background=“#43eeff”
android:gravity=“center”
android:text=“1” />
<TextView
android:id=“@+id/textview2”
android:layout_width=“120dp”
android:layout_height=“60dp”
android:layout_margin=“2dp”
android:background=“#ef3344”
android:gravity=“center”
android:text=“2” />
<TextView
android:id=“@+id/textview3”
android:layout_width=“120dp”
android:layout_height=“90dp”
android:layout_margin=“2dp”
android:background=“#ee998f”
android:gravity=“center”
android:text=“3” />
<TextView
android:id=“@+id/textview4”
android:layout_width=“120dp”
android:layout_height=“100dp”
android:layout_margin=“2dp”
android:background=“#eeff22”
android:gravity=“center”
android:text=“4” />
<TextView
android:id=“@+id/textview5”
android:layout_width=“120dp”
android:layout_height=“80dp”
android:layout_margin=“2dp”
android:background=“#3322ff”
android:gravity=“center”
android:text=“5” />
</com.google.android.flexbox.FlexboxLayout>
运行后的效果如下
好像并没有换行啊,别急骚年,接下来我们一一介绍FlexboxLayout
的一些常用属性
FlexboxLayout 常用属性
flexDirection
flexDirection
属性决定主轴项目排列方向。类似LinearLayout
的 vertical
和 horizontal
,但是FlexboxLayout
更加强大,不仅支持横向和纵向还可以设置不同的排列的起点。
-
row
(默认值):主轴为水平方向,起点在左端 -
row_reverse
:主轴为水平方向,起点在右端。 -
column
:主轴为垂直方向,起点在上沿 -
column_reverse
:主轴为垂直方向,起点在下沿
我们添加flexDirection
属性,设置未纵向并且起点在下端,在xml添加属性
app:flexDirection=“column_reverse”
可以看到项目是从底部开始由下而上排列的。
flexWrap
默认FlexboxLayout
和LinearLayout
一样是不带换行属性的,但是flexWrap
属性可以支持换行排列。这就是FlexboxLayout
方便的地方了。换行方式有两种,一种是按项目排列方向换行,一种是反方向换行
-
nowrap
:不换行 -
wrap
:按正常方向换行 -
wrap_reverse
:按反方向换行
我们设置按照正常方向换行,添加属性
app:flexWrap=“wrap”
justifyContent
justifyContent
属性定义了项目在主轴上的对齐方式。
-
flex_start
(默认值):左对齐 -
flex_end
:右对齐 -
center
: 居中 -
space_between
:两端对齐,项目之间的间隔都相等。 -
space_around
:每个项目两侧的间隔相等。项目之间的间隔比项目与边框的间隔大一倍。
默认是左对齐的,现在我们设置右对齐,xml添加属性
app:justifyContent=“flex_end”
如果需要在项目的排列方向上均分剩余的空间怎么办呢?很简单space_around
属性就是这样的,效果如下
alignItems
alignItems
属性定义项目在副轴轴上如何对齐,我们通过一张图来了解这个属性比较直观一点。
-
flex-start
:交叉轴的起点对齐。 -
flex-end
:交叉轴的终点对齐。 -
center
:交叉轴的中点对齐。 -
baseline
: 项目的第一行文字的基线对齐。 -
stretch
(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
这也是为什么我们的每一个项目的高度都是不相同的,但是可以看到前面每个项目的高度都是一样的,因为默认属性stretch
让每个项目的高度设置为了填满容器的高度(这里的高度是指同一轴上的最高高度) 现在我们设置对齐方式为中心对齐,添加属性
app:alignItems=“center”
可以看到是根据每个项目的中心对齐,这里单独说一下baseline
属性,熟悉ConstraintLayout
的同学应该比较好理解这个属性,其实就是按照项目内的文本线来对齐项目。效果如下
可以看到项目对齐是按照项目内的文本基线来对齐的。很好理解!需要注意的是项目中如果有的没有文本基线,那么默认他的基线就是左上角也就是起点左右位置
alignContent
alignContent
属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
-
flex_start
:与交叉轴的起点对齐。 -
flex_end
:与交叉轴的终点对齐。 -
center
:与交叉轴的中点对齐。 -
space_between
:与交叉轴两端对齐,轴线之间的间隔平均分布。 -
space_around
:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。 -
stretch
(默认值):轴线占满整个交叉轴。
alignContent
和justifyContent
其实里面的属性值都是一样的 ,justifyContent
是设置主轴的对齐方式,alignContent
是设置多个轴的对齐方式,通俗的讲可以理解为比如是项目是水平换行,justifyContent
就是设置垂直方向的对齐方式,alignContent
就是设置水平方向的对齐方式。现在我们想让每个项目距离上右下左的距离是一样的,需要把alignContent
和justifyContent
都设置为space_around
就可以了,
app:alignContent=“space_around”
app:justifyContent=“space_around”
最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
存中…(img-GEuEhJvY-1714275527098)]
最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
[外链图片转存中…(img-c9Kp3kba-1714275527099)]
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!