高级UI晋升之自定义View实战(八)
更多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实战(八)的更多相关文章
- 高级UI晋升之自定义View实战(六)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...
- 高级UI晋升之自定义View实战(九)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 1.前言: 本文采用自定义view的方法来实现一键清除的动画这个功能. 2.效果 ...
- 高级UI晋升之自定义View实战(五)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从自定义View利器Canvas和Paint来进行详解 一.Canvas ...
- 高级UI晋升之自定义view实战(七)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章自定义ViewGroup实现瀑布流效果来进行详解dispatchTouch ...
- 高级UI晋升之常用View(三)中篇
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...
- 高级UI晋升之常用View(三)上篇
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将先从以下两个内容来介绍常用View: [RecycleView] [Ca ...
- 高级UI晋升之常用View(三)下篇
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从WebView来介绍常用View: 一.WebView介绍 Andro ...
- 高级UI晋升之View渲染机制(二)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...
- Android自定义View实战(SlideTab-可滑动的选择器)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52178553 本文出自:[openXu的博客] 目录: 初步分析重写onDraw绘制 重写o ...
随机推荐
- TypeError: write() argument must be str, not bytes报错
TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...
- mapreduce实验
代码: public class WordCount { public static void main(String[] args) throws IOException, ClassNotFoun ...
- MVC5使用SignalR进行双向通信 (1)
@a604572782 2015-08-10 09:01 字数 2133 阅读 1245 MVC5使用SignalR进行双向通信 (1) 配置SignalR 在NuGet中通过 install-pac ...
- Codeforces Fix a Tree
Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let' ...
- 力扣 —— Two Sum ( 两数之和) python实现
题目描述: 中文: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...
- Java高频经典面试题(第一季)一:自增的分析
package will01; public class testZiZeng { public static void main(String[] args) { int i = 1; i = i ...
- QT问题解决
1.pro文件下各个变量的含义 https://www.zybuluo.com/breakerthb/note/582395 2.如何在pro文件中导入其他的库 https://blog.csdn.n ...
- 容器和Docker
一.容器 1.虚拟机和容器的区别 (1)为什么要用docker 服务器端开发/部署: 实现更轻量级的虚拟化,方便快速部署, 对于部署来说可以极大的减少部署的时间成本和人力成本 Docker支持将应用打 ...
- redis单节点安装及cluster的安装
单点安装 wget http://download.redis.io/releases/redis-4.0.2.tar.gz tar zxvf redis-4.0.1.tar.gz -C /usr/l ...
- Eclipse如何构建(普通web)Maven工程
进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) 一.Maven项目的新建 1.鼠标右键---->New--- ...