安卓流式布局实现记录

效果图:

1、导入第三方控件

复制代码
    implementation 'com.google.android:flexbox:1.1.0'

2、布局中使用

XML 复制代码
  <com.google.android.flexbox.FlexboxLayout
       android:id="@+id/baggageFl"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:flexWrap="wrap"
       app:justifyContent="space_evenly" >

                         

  </com.google.android.flexbox.FlexboxLayout>

3、流式布局中内容的填充

这里有两种实现方式:xml中直接写和动态添加view

第一种和正常写布局一样,只要把内容控件写进去就行,就不说了,主要说第二种

举例说明:

创建一个子布局:item_baggage.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<com.hjq.shape.layout.ShapeConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingVertical="@dimen/dp_5"
    android:paddingHorizontal="@dimen/dp_8"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.hjq.shape.view.ShapeTextView
        android:id="@+id/baggageNameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_10"
        android:layout_marginEnd="@dimen/dp_10"
        android:paddingHorizontal="@dimen/dp_15"
        android:paddingVertical="@dimen/dp_5"
        android:text="----"
        android:textColor="#333333"
        android:textSize="@dimen/sp_16"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shape_radius="@dimen/dp_10"
        app:shape_solidColor="#66FFD374"
        app:shape_strokeColor="#FFD374"
        app:shape_strokeSize="@dimen/dp_1" />

    <ImageView
        android:id="@+id/closeImg"
        android:layout_width="@dimen/dp_20"
        android:layout_height="@dimen/dp_20"
        android:src="@mipmap/close_icon"
        app:layout_constraintBottom_toTopOf="@+id/baggageNameTv"
        app:layout_constraintEnd_toEndOf="@+id/baggageNameTv"
        app:layout_constraintStart_toEndOf="@+id/baggageNameTv"
        app:layout_constraintTop_toTopOf="@+id/baggageNameTv" />
</com.hjq.shape.layout.ShapeConstraintLayout>

2、动态添加view到FlexboxLayout中

Kotlin 复制代码
    private fun addLayoutToFlexboxLayout(strList: List<String>) {

        for (str in strList) {
            val itemView = LayoutInflater.from(this).inflate(R.layout.item_baggage, null)
            itemView.findViewById<ShapeTextView>(R.id.baggageNameTv).text = str
            binding.baggageFl.addView(itemView)
        }
    }

详细学习:FlexboxLayout使用(Google官方实现流式布局控件)_com.google.android:flexbox-CSDN博客

相关推荐
一笑的小酒馆35 分钟前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺3 小时前
Android BLE 扫描完整实战
android
TeleostNaCl5 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang95276 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_915918417 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong9517 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海7 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿7 小时前
毕业三年后,我离职了
android·面试
编程乐学8 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐9 小时前
Android14 init.rc中on boot阶段操作4
android