Android 自定义ViewGroup 实现流式布局
2015-08-05 14:42
477 查看
学自鸿洋(hyman)的imooc视频
宽度不足自动换行。
FlowLayout
main.xml
ViewGroup.MarginLayoutParams
系统中ViewGroup的子类对应的LayoutParams都继承了MarginLayoutParams
lp.leftMargin; topMargin, rightMargin, bottomMargin
view.getPaddingLeft 类似这样的方法,能拿到padding值
我的自定义View项目地址: https://github.com/aa86799/MyCustomView (欢迎start&fork)
本文地址:https://github.com/aa86799/MyCustomView/tree/master/flowlayout
宽度不足自动换行。
FlowLayout
/** * author : stone * email : aa86799@163.com * time : 15/8/4 15 08 */ public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { this(context, null); } public FlowLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (changed) { int w = getWidth(), h = getHeight(); int tw = 0, th = 0; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if (tw + child.getMeasuredWidth() <= w) { } else { tw = 0; th += child.getMeasuredHeight(); } child.layout(tw, th, tw+child.getMeasuredWidth(), th+child.getMeasuredHeight()); tw += child.getMeasuredWidth(); if (child instanceof TextView) { ((TextView)child).setTextColor(getColor()); } } } } /** * 随机颜色 * @return */ private int getColor() { StringBuilder sb = new StringBuilder(); Random random = new Random(); String temp; for (int i = 0; i < 3; i++) { temp = Integer.toHexString(random.nextInt(0xFF)); if (temp.length() == 1) { temp = "0" + temp; } sb.append(temp); } return Color.parseColor("#" + sb.toString()); } }这里没考虑 子view高度不同的情况,也没考虑padding、margin的情况。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.stone.flowlayout.view.FlowLayout android:id="@+id/fl_layout" android:layout_width="match_parent" android:layout_height="match_parent"> </com.stone.flowlayout.view.FlowLayout> </LinearLayout>
ViewGroup.MarginLayoutParams
系统中ViewGroup的子类对应的LayoutParams都继承了MarginLayoutParams
lp.leftMargin; topMargin, rightMargin, bottomMargin
view.getPaddingLeft 类似这样的方法,能拿到padding值
我的自定义View项目地址: https://github.com/aa86799/MyCustomView (欢迎start&fork)
本文地址:https://github.com/aa86799/MyCustomView/tree/master/flowlayout
相关文章推荐
- 深入解析Android的自定义布局
- Android L 5.0 上紧急电话EmergencyCall与普通电话在MO流程上的区别
- Android学习笔记——声明
- Android init进程——属性服务
- android 问题集锦
- Android编译过程详解(三)
- Android 图形:绘制渐变色奥运五环图形,游戏文字,验证码,Matrix旋转,缩放,倾斜,平移等
- Android编译过程详解(二)
- Android编译过程详解(一)
- Android优化之代码优化
- android studio sdk更新 (2015-8-5)
- Android学习笔记——Android生命周期
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
- 终结android开发关于R文件的报错
- 一个简单的ExpandableView例子
- 9个Web工具提高Android开发效率
- android Failed to pull selection:在cmd里使用命令pull复制文件 \platf orm-tools>adb pull /storage/1.mp4 e:/1.mp
- Android 谷歌 开源 通信框架 VOLLEY(四)——定制请求
- Tabbed Activity
- Android 实现断点续传