android: 使用flexbox布局
一,安装第三方库
https://mvnrepository.com/artifact/com.google.android.flexbox/flexbox
编辑build.gralde,添加:
// https://mvnrepository.com/artifact/com.google.android.flexbox/flexbox
runtimeOnly 'com.google.android.flexbox:flexbox:3.0.0'
然后点击Sync Now
二,代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.Flex1Activity">
<com.google.android.flexbox.FlexboxLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexDirection="row"
app:justifyContent="space_between"
app:flexWrap="wrap">
<TextView
style="@style/TextStyle"
android:text="你真的这样的世界吗"/>
<TextView
style="@style/TextStyle"
android:text="你真的了解中国吗" />
<TextView
style="@style/TextStyle"
android:text="你真的了解你自己吗"/>
<TextView
style="@style/TextStyle"
android:text="你了解你的父母吗" />
<TextView
style="@style/TextStyle"
android:text="你..." />
<TextView
style="@style/TextStyle"
android:text="我...." />
<TextView
style="@style/TextStyle"
android:text="他...." />
</com.google.android.flexbox.FlexboxLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
说明:
app:flexDirection的取值:
"column" //主轴方向按竖直(列)方向排版。
column_reverse : 主轴方向按竖直(列)方向反向排版(从下向上)。
row : 主轴方向按水平(行)方向排版,默认就是这个属性 默认值
row_reverse : 主轴方向按水平(行)方向反向排版
app:justifyContent="flex_start" 默认值
flex_start: 左对齐,默认值。
flex_end : 右对齐。
center:居中对齐。
space_between: 两端对齐。
space_around : 分散对齐。
space_evenly:子元素在一行内均匀分布空间。
三,测试效果:
app:flexDirection="row" 默认值

app:flexDirection="column"

justifyContent="flex_end"

justifyContent="center"

justifyContent="space_between"

浙公网安备 33010602011771号