当前位置:网站首页>Constrained layout flow
Constrained layout flow
2022-07-05 10:11:00 【A bird carved in the desert】
Flow Fluid layout
There are many ways to implement streaming layout , such as RecycleView Of StaggeredGridLayoutManager
, perhaps Customize the dynamic calculation method of layout
etc. , No matter which implementation , Are all behind ConstraintLayout Provided Flow Fluid layout
More flexible , It's simpler .
base_component_learn/ConstraintFlowActivity.java at master · buder-cp/base_component_learn · GitHub
Here we add dynamically , The effect is as follows :
The code is very simple , That is, you can use dynamic add layout :
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[]{ " It's amazing how dark the silver man is... How dark the silver man is ", " Freely flowing style of writing ", " tearful ", " Good words ",
" Frost ", " Cuitong, cuitong ", " beam with joy ", " Maggie ", " Rong ", " Words describing friendship ",
" A collection of beautiful words ", " smile into each other 's eyes ", " Smile ", " Esteem, ", " look very happy ", " Charming ", " The darkness at ", " Day and night, day and night ", " It wins, ", "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 The code contains only one flow that will do :
<?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>
Write in native code flow Is streaming layout very simple .
Specifically Flow You can see some specific properties of ConstraintLayout 2.0 Analysis of new features ( One )--Flow Fluid layout - Simple books
The core is to use attributes :app:constraint_referenced_ids
take 7 individual ImageView Restrain yourself .
<?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>
边栏推荐
- Energy momentum: how to achieve carbon neutralization in the power industry?
- 一个程序员的职业生涯到底该怎么规划?
- Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
- 善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
- MySQL character type learning notes
- 天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
- How to correctly evaluate video image quality
- [app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
- The Alipay in place function can't be found, and the Alipay in place function is offline
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
猜你喜欢
@SerializedName注解使用
驱动制造业产业升级新思路的领域知识网络,什么来头?
[system design] index monitoring and alarm system
Kotlin Compose 与原生 嵌套使用
How to get the STW (pause) time of GC (garbage collector)?
能源势动:电力行业的碳中和该如何实现?
View Slide
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
[200 opencv routines] 219 Add digital watermark (blind watermark)
随机推荐
【C语言】动态内存开辟的使用『malloc』
Windows uses commands to run kotlin
Charm of code language
Using directive in angualr2 to realize that the picture size changes with the window size
90%的人都不懂的泛型,泛型的缺陷和应用场景
[C language] the use of dynamic memory development "malloc"
《微信小程序-基础篇》小程序中的事件与冒泡
字节跳动面试官:一张图片占据的内存大小是如何计算
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Coordinate system of view
天龙八部TLBB系列 - 单体技能群伤
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
能源势动:电力行业的碳中和该如何实现?
The essence of persuasion is to remove obstacles
Apache DolphinScheduler 入门(一篇就够了)
一个程序员的职业生涯到底该怎么规划?
程序员搞开源,读什么书最合适?
C#函数返回多个值方法
Tdengine already supports the industrial Intel edge insight package
Generics, generic defects and application scenarios that 90% of people don't understand