ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android FlexboxLayout布局属性详解,最新手淘Android高级面试题及答案

2021-12-28 11:31:19  阅读:145  来源: 互联网

标签:面试题 layout app FlexboxLayout id 对齐 Android android 属性


使用

项目中添加依赖

dependencies {

compile ‘com.google.android:flexbox:0.2.6’

}

如果是在RecycleView中使用则添加

dependencies {

compile ‘com.google.android:flexbox:0.3.0-alpha3’

}

Alpha版本包括RecyclerView的集成

XML中添加布局

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

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属性决定主轴项目排列方向。类似LinearLayoutverticalhorizontal,但是FlexboxLayout更加强大,不仅支持横向和纵向还可以设置不同的排列的起点。

  • row(默认值):主轴为水平方向,起点在左端

  • row_reverse:主轴为水平方向,起点在右端。

  • column:主轴为垂直方向,起点在上沿

  • column_reverse:主轴为垂直方向,起点在下沿

我们添加flexDirection属性,设置未纵向并且起点在下端,在xml添加属性

app:flexDirection=“column_reverse”

可以看到项目是从底部开始由下而上排列的。

flexWrap

默认FlexboxLayoutLinearLayout一样是不带换行属性的,但是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(默认值):轴线占满整个交叉轴。

alignContentjustifyContent其实里面的属性值都是一样的 ,justifyContent是设置主轴的对齐方式,alignContent是设置多个轴的对齐方式,通俗的讲可以理解为比如是项目是水平换行,justifyContent就是设置垂直方向的对齐方式,alignContent就是设置水平方向的对齐方式。现在我们想让每个项目距离上右下左

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

的距离是一样的,需要把alignContentjustifyContent都设置为space_around就可以了,

app:alignContent=“space_around”

app:justifyContent=“space_around”

子元素属性

除以上之外,FlexboxLayout不仅有自身的属性,还可以设置子元素的属性。这也是FlexboxLayout能完成聪明布局的原因之一

layout_order

lignContent`就是设置水平方向的对齐方式。现在我们想让每个项目距离上右下左

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

的距离是一样的,需要把alignContentjustifyContent都设置为space_around就可以了,

app:alignContent=“space_around”

app:justifyContent=“space_around”

[外链图片转存中…(img-f1H6e97n-1640661985421)]

子元素属性

除以上之外,FlexboxLayout不仅有自身的属性,还可以设置子元素的属性。这也是FlexboxLayout能完成聪明布局的原因之一

layout_order

标签:面试题,layout,app,FlexboxLayout,id,对齐,Android,android,属性
来源: https://blog.csdn.net/m0_64604042/article/details/122189111

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有