安卓流式布局实现记录

效果图:

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博客

相关推荐
安卓开发者6 小时前
Android RxJava 组合操作符实战:优雅处理多数据源
android·rxjava
阿华的代码王国6 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
一条上岸小咸鱼6 小时前
Kotlin 基本数据类型(三):Booleans、Characters
android·前端·kotlin
Jerry说前后端7 小时前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
alexhilton7 小时前
深入浅出着色器:极坐标系与炫酷环形进度条
android·kotlin·android jetpack
一条上岸小咸鱼13 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto14 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆14 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren14 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.18 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架