当前位置:网站首页>ConstraintLayout的流式布局Flow
ConstraintLayout的流式布局Flow
2022-07-05 09:41:00 【沙漠一只雕得儿得儿】
Flow流式布局
流式布局的实现方式有很多种,比如RecycleView的StaggeredGridLayoutManager,或者自定义布局动态计算的方式等,无论哪种实现,都不如ConstraintLayout提供的Flow流式布局来的更灵活,更简单。
base_component_learn/ConstraintFlowActivity.java at master · buder-cp/base_component_learn · GitHub
这里我们进行动态添加,效果如下:

代码非常简单,即使用动态添加布局即可:
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.helper.widget.Flow;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.ArrayList;
public class ConstraintFlowActivity extends AppCompatActivity {
private ConstraintLayout mainLayout;
private Flow flow;
private int[] idsss = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
private String[] strings = new String[]{ "拍案叫绝银汉迢迢暗度银汉迢汉迢迢暗度", "行云流水", "花花", "词语好词",
"如霜", "翠彤翠彤翠彤", "眉飞色舞", "梦琪", "蓉", "描写友情的词",
"优美词语集锦", "相视而笑", "笑容", "推心置腹", "喜上眉梢", "可掬", "银汉迢迢暗度", "又岂在朝朝暮暮又岂", "便胜却", "ads"};
private ArrayList<Integer> arrayList = new ArrayList<Integer>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.constraint_exercise);
mainLayout = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.constraint_exercise, null);
setContentView(mainLayout);
for (int i = 1; i < 20; i++) {
arrayList.add(i);
mainLayout.addView(generateView(i));
}
flow = findViewById(R.id.constrain_flows);
// flow.setMaxElementsWrap(3);
// flow.setHorizontalAlign();
flow.setWrapMode(1);
flow.setReferencedIds(idsss);
}
private TextView generateView(int i) {
TextView textView = new TextView(getApplicationContext());
textView.setId(i);
textView.setPadding(12, 5, 12, 5);
textView.setTextColor(Color.BLACK);
textView.setBackgroundResource(R.drawable.vip_course_selector_bg);
textView.setText(strings[i]);
return textView;
}
}XML代码仅包含一个flow即可:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/constrain_flows"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
app:layout_constraintTop_toTopOf="parent"
app:flow_horizontalGap="10dp"
app:flow_verticalGap="10dp"
app:flow_maxElementsWrap="4"
app:flow_wrapMode="aligned" />
</androidx.constraintlayout.widget.ConstraintLayout>使用原生代码写flow流式布局是不是很简单呢。
具体Flow的一些具体属性可以看ConstraintLayout 2.0新特性解析(一)--Flow流式布局 - 简书
核心就是使用属性:app:constraint_referenced_ids将7个ImageView约束起来。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.helper.widget.Flow
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:flow_horizontalGap="10dp"
app:flow_verticalGap="10dp"
app:flow_wrapMode="none"
app:constraint_referenced_ids="image1,image2,image3,image4,image5,image6,image7" />
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>边栏推荐
- 90%的人都不懂的泛型,泛型的缺陷和应用场景
- 解决idea调试过程中liquibase – Waiting for changelog lock….导致数据库死锁问题
- How to implement complex SQL such as distributed database sub query and join?
- MySQL digital type learning notes
- Generics, generic defects and application scenarios that 90% of people don't understand
- 如何写出高质量的代码?
- TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
- Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
- H. 265 introduction to coding principles
- 双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
猜你喜欢

Advanced opencv:bgr pixel intensity map

.Net之延迟队列

Wechat applet - simple diet recommendation (3)

Wechat applet - simple diet recommendation (4)
![[tips] get the x-axis and y-axis values of cdfplot function in MATLAB](/img/08/2d039df6ea3ace8685512b2af8281d.png)
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB

一种用于干式脑电图的高密度256通道电极帽
![[200 opencv routines] 219 Add digital watermark (blind watermark)](/img/ac/f6d16e74538bd589a1f6faee08d59a.png)
[200 opencv routines] 219 Add digital watermark (blind watermark)

让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...

Comment obtenir le temps STW du GC (collecteur d'ordures)?

Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
随机推荐
How to implement complex SQL such as distributed database sub query and join?
90%的人都不懂的泛型,泛型的缺陷和应用场景
Comment obtenir le temps STW du GC (collecteur d'ordures)?
Pagoda panel MySQL cannot be started
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
如何获取GC(垃圾回收器)的STW(暂停)时间?
Wechat applet - simple diet recommendation (3)
A high density 256 channel electrode cap for dry EEG
Apache DolphinScheduler 系统架构设计
ArcGIS Pro creating features
Kotlin Compose 与原生 嵌套使用
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Kotlin Compose 多个条目滚动
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
Design and exploration of Baidu comment Center
Wechat applet - simple diet recommendation (4)
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
如何写出高质量的代码?