常用类继承自ViewGroup的写法
来源:互联网 发布:linux 查看磁盘阵列 编辑:程序博客网 时间:2024/04/30 05:56
通常情况下要写一个自定义的类继承自ViewGroup,仅需重写OnLayout这个方法就可以了, 到时再需要再上来copy
public class MyViewGroup extends ViewGroup {
private final static int VIEW_MARGIN = 2;//定义一个边界值
private int maxWidth = 0;
private int maxHeight = 60;
public MyViewGroup(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
for (int index = 0; index < getChildCount(); index++) {
final View child = getChildAt(index);
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
final int count = getChildCount();
int row = 0;// which row lay you view relative to parent
int lengthX = arg1; // right position of child relative to parent
int lengthY = arg2; // bottom position of child relative to parent
for(int i = 0 ; i < count ; i++){
final View child = this.getChildAt(i);
int width = child.getMeasuredWidth();
// int height = child.getMeasuredHeight();
int height = maxHeight; //限制子节点的高度
lengthX += width + VIEW_MARGIN;
lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height + arg2;
if(width + VIEW_MARGIN > maxWidth){
maxWidth = width + VIEW_MARGIN;
}
if(lengthX > arg3){
lengthX = width + VIEW_MARGIN + arg1;
row ++;
lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height + arg2;
}
child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
}
}
}
- 常用类继承自ViewGroup的写法
- 继承ViewGroup的自定义类
- 继承ViewGroup的自定义类
- 自定义viewGroup的常见写法
- Anroid自定义控件/继承自ViewGroup的刷新控件——LinearLayout
- 常用的一句话 多种写法(转自雕牌)
- 继承viewgroup
- 继承ViewGroup
- Android继承自View和ViewGroup,自定义控件
- android 自定义View学习总结-继承自ViewGroup
- android 自定义View学习总结-继承自ViewGroup(2)
- 自连接的写法
- 继承ViewGroup重写onMeasure方法的详解
- 继承ViewGroup需要重写的方法
- 自定义view继承viewgroup的解释
- 继承ViewGroup重写onMeasure方法的详解
- Maven依赖继承的写法
- Qt desiner 的继承写法
- Linux内核中的PAE意义
- 穿越NAT的p2p通信方法研究
- JS中showModalDialog 详细使用
- CEGUI
- Scintilla开源库使用指南(一)
- 常用类继承自ViewGroup的写法
- Keil MDK中使用pc-lint的详细方法
- uva 185
- XP系统下设置共享文档
- Python标准库(非常经典的各种模块介绍)
- A10 Nand Flash空间新增一个共享分区方法
- Java中获取当前运行代码的类名、方法名、行号
- 网页相关小功能实现
- Syslog-NG日志服务器安装与配置