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

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

 
 

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

一、引入资源

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

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

  1. allprojects {
  2. repositories {
  3. maven { url 'https://jitpack.io' }
  4. }
  5. }
  6. dependencies {
  7. compile 'com.github.zrunker:ZFlowLayout:v1.0'
  8. }

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

  1. <repositories>
  2. <repository>
  3. <id>jitpack.io</id>
  4. <url>https://jitpack.io</url>
  5. </repository>
  6. </repositories>
  7. <dependency>
  8. <groupId>com.github.zrunker</groupId>
  9. <artifactId>ZFlowLayout</artifactId>
  10. <version>v1.0</version>
  11. </dependency>

二、使用

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

1、引入布局文件

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

2、动态添加子控件

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. FlowLayout flowLayout = findViewById(R.id.flowlayou);
  7. LayoutInflater inflater = LayoutInflater.from(this);
  8. for (int i = 0; i < 20; i++) {
  9. TextView textView = (TextView) inflater.inflate(R.layout.tag_textview, flowLayout, false);
  10. if (i == 3)
  11. textView.setText("Android1111" + i);
  12. else if (i == 6)
  13. textView.setText("Jave1111" + i);
  14. else if (i == 10)
  15. textView.setText("kotlin1111" + i);
  16. else
  17. textView.setText("测试" + i);
  18. flowLayout.addView(textView);
  19. }
  20. }
  21. }

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

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

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

  1. <cc.ibooker.zflowlayoutlib.FlowLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <TextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_margin="5dp"
  9. android:background="@drawable/gridview_selector"
  10. android:padding="5dp"
  11. android:text="张三" />
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_margin="5dp"
  16. android:background="@drawable/gridview_selector"
  17. android:padding="5dp"
  18. android:text="李四张三" />
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_margin="5dp"
  23. android:background="@drawable/gridview_selector"
  24. android:padding="5dp"
  25. android:text="王五李四张三" />
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_margin="5dp"
  30. android:background="@drawable/gridview_selector"
  31. android:padding="5dp"
  32. android:text="赵六王五李四张三" />
  33. <TextView
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_margin="5dp"
  37. android:background="@drawable/gridview_selector"
  38. android:padding="5dp"
  39. android:text="孙七赵六王五李四张三" />
  40. <TextView
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_margin="5dp"
  44. android:background="@drawable/gridview_selector"
  45. android:padding="5dp"
  46. android:text="周八孙七赵六王五李四张三" />
  47. <TextView
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_margin="5dp"
  51. android:background="@drawable/gridview_selector"
  52. android:padding="5dp"
  53. android:text="吴九周八孙七赵六王五李四张三" />
  54. <TextView
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_margin="5dp"
  58. android:background="@drawable/gridview_selector"
  59. android:padding="5dp"
  60. android:text="郑十吴九周八孙七赵六王五李四张三" />
  61. <TextView
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:layout_margin="5dp"
  65. android:background="@drawable/gridview_selector"
  66. android:padding="5dp"
  67. android:text="Tom" />
  68. <TextView
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:layout_margin="5dp"
  72. android:background="@drawable/gridview_selector"
  73. android:padding="5dp"
  74. android:text="zrunker" />
  75. <TextView
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:layout_margin="5dp"
  79. android:background="@drawable/gridview_selector"
  80. android:padding="5dp"
  81. android:text="Android" />
  82. </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. 不小心执行了 rm -f,先别急着跑路

    作者:justmine http://www.cnblogs.com/justmine/p/10359186.html 前言 每当我们在生产环境服务器上执行rm命令时,总是提心吊胆的,因为一不小心执行 ...

  2. nginx之域名重定向

    一般网站默认的访问端口为80,当多个域名指向同一个服务器IP时,可以nginx进行重定向,分别指向不同的目的地址或其他主机. 在nginx目录下的conf/vhost子目录下建两个conf文件,hos ...

  3. Tkinter初体验

    一.基本步骤 1.导入Tkinter模块 2.创建根窗口 3.填充组件 4.组件关联逻辑 5.进入主循环 二.Code #coding:utf-8 ''' 网关流量校验器 @author: Hongz ...

  4. 如何解决“ VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid.c:386 bugNr=1036521”

    第一次装虚拟机,装centos7遇到的坑: 1. 出现 “VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid ...

  5. java应用之openfire入门篇

    前言 openfire是一个聊天服务端,好比qq服务端.本质是个socker server. openfire通讯协议是 xmpp  ,什么是xmpp参考百科 https://baike.baidu. ...

  6. EventBus总结(原)

    1.EventBus的作用 EventBus is a publish/subscribe event bus for Android and Java. EventBus可以被用来在各种自定义的监听 ...

  7. Mongo导出、导入

    1.mongodb 数据导出: connection options: /h, /host:<hostname> mongodb host to connect to (setname/h ...

  8. spring mvc 整合 druid

    环境: ubuntu eclipse maven 一. pom.xml 加入druid 依赖 <!-- https://mvnrepository.com/artifact/com.alibab ...

  9. Es学习第二课, ES安装和客户端使用

    Elasticsearch安装依赖于jdk,所以大家先保证自己电脑安装好Java环境(JDK7或更高版本),并配置好环境变量:这块的配置我就不细讲了,不管是Linux还是Windows,这块的安装和配 ...

  10. Hadoop(一)阿里云hadoop集群配置

    集群配置 三台ECS云服务器 配置步骤 1.准备工作 1.1 创建/bigdata目录 mkdir /bigdatacd /bigdatamkdir /app 1.2修改主机名为node01.node ...