更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680
本篇文章自定义流式布局来进行介绍:

一般常见的流式布局由两种,一种是横向的个数固定,列表按照竖向进行排列。另一种是横向先排,横向排满之后再竖向排列。而本框架实现是以第二种方式进行处理。

 
 

那么这个框架到底该如何使用呢?

一、引入资源

这里提供两种方式,引入资源文件。

1、在build.gradle文件中添加以下代码:

allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
} dependencies {
compile 'com.github.zrunker:ZFlowLayout:v1.0'
}

2、在maven文件中添加以下代码:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories> <dependency>
<groupId>com.github.zrunker</groupId>
<artifactId>ZFlowLayout</artifactId>
<version>v1.0</version>
</dependency>

二、使用

使用该框架,只需要两步即可。

1、引入布局文件

<cc.ibooker.zflowlayoutlib.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flowlayou"
android:layout_width="match_parent"
android:layout_height="match_parent" />

2、动态添加子控件

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); FlowLayout flowLayout = findViewById(R.id.flowlayou); LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < 20; i++) {
TextView textView = (TextView) inflater.inflate(R.layout.tag_textview, flowLayout, false);
if (i == 3)
textView.setText("Android1111" + i);
else if (i == 6)
textView.setText("Jave1111" + i);
else if (i == 10)
textView.setText("kotlin1111" + i);
else
textView.setText("测试" + i);
flowLayout.addView(textView);
}
}
}

其中tag_textview为自定义子控件的布局文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp" />

当然也可以把FlowLayout直接当做一个ViewGroup在布局文件中直接加入子控件,就不需要动态的添加子控件,如下:

<cc.ibooker.zflowlayoutlib.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="王五李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="赵六王五李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="孙七赵六王五李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="周八孙七赵六王五李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="吴九周八孙七赵六王五李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="郑十吴九周八孙七赵六王五李四张三" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="Tom" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="zrunker" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/gridview_selector"
android:padding="5dp"
android:text="Android" /> </cc.ibooker.zflowlayoutlib.FlowLayout>

最后看看效果图:

 
流式布局效果图

更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680
原文链接https://www.jianshu.com/p/ae8ffdab753d

高级UI晋升之自定义View实战(八)的更多相关文章

  1. 高级UI晋升之自定义View实战(六)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...

  2. 高级UI晋升之自定义View实战(九)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 1.前言: 本文采用自定义view的方法来实现一键清除的动画这个功能. 2.效果 ...

  3. 高级UI晋升之自定义View实战(五)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从自定义View利器Canvas和Paint来进行详解 一.Canvas ...

  4. 高级UI晋升之自定义view实战(七)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章自定义ViewGroup实现瀑布流效果来进行详解dispatchTouch ...

  5. 高级UI晋升之常用View(三)中篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...

  6. 高级UI晋升之常用View(三)上篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将先从以下两个内容来介绍常用View: [RecycleView] [Ca ...

  7. 高级UI晋升之常用View(三)下篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从WebView来介绍常用View: 一.WebView介绍 Andro ...

  8. 高级UI晋升之View渲染机制(二)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...

  9. Android自定义View实战(SlideTab-可滑动的选择器)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52178553 本文出自:[openXu的博客] 目录: 初步分析重写onDraw绘制 重写o ...

随机推荐

  1. Python:我可以使用类变量作为线程锁吗?

    我正在考虑使用类变量作为线程锁,因为我不想在全局变量中定义锁并且还想防止死锁.这实际上有用吗?例: import threading class A(object): lock = threading ...

  2. css 图片有间隔多个Img标签之间的间隙

    今天写css时发现,图片加起来刚好是900px的三张图片,不能在一个900px宽容器放下,因为图片之间有间隔,我猜是浏览器把两个img标签之间的空格当成了空白节点. 在网上找到了几个不错的解决方法: ...

  3. 常用Message Queue对比

    目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量 ...

  4. HTTP Request 422 Unprocessable Entity

    最近接了一个接口,在调用接口时,返回这个错误代码. 百度到的解释是:请求格式正确,但是由于含有语义错误,无法响应. 开始一直在纠结,语义错误到底是什么?对照了无数次参数名,传参方式,无解. 后来用Fi ...

  5. 获取年月日格式为yyyy-m-d简单写法

    方法:通过日期函数toLocaleString()获取.new Date().toLocaleString( );  //"2019/9/1 上午11:20:23" 获取年月日方法 ...

  6. Bata冲刺第三天

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...

  7. 转载:Think in AngularJS:对比jQuery和AngularJS的不同思维模式(大漠穷秋)

    导言 stackoverflow上有一个人问了一个问题:如果我有jQuery背景,我应该如何切换到AngularJS的思维模式? 有一个回复非常经典,获得了两千多票. 为了让国内开发者也能领略到其中的 ...

  8. Spring中自动装配的模式

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484037.html 自动装配模式 Reference https://docs.spring.io ...

  9. 【leetcode】934. Shortest Bridge

    题目如下: In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connecte ...

  10. Database基础(四):密码恢复及设置、 用户授权及撤销、数据备份与恢复、MySQL管理工具

    一.密码恢复及设置 目标: 本案例要求熟悉MySQL管理密码的控制,完成以下任务操作: 练习重置MySQL管理密码的操作 通过正常途径设置MySQL数据库的管理密码 步骤: 步骤一:重置MySQL管理 ...