extends android.view.ViewGroup两种实现
/* private int measureHeight(int heightMeasureSpec) {
int count = getChildCount();
int rowCount = 1;
int left = 0;
int top = 0;
int right = left + getMeasuredWidth();
int childHeight = (count == 0) ? 0 : getChildAt(0).getMeasuredHeight();
int childWidth;
int totalWidth = left;
int totalHeight = top;
// 最后两个child是EditText和ImageButton
int editWidth = getChildAt(count - 2).getMeasuredWidth();
int buttonWidth = getChildAt(count - 1).getMeasuredWidth();
for (int i = 0; i < count - 2; i++) {
childWidth = getChildAt(i).getMeasuredWidth();
// 加上当前childView超出右边界换行。
if (totalWidth + childWidth > right) {
rowCount++;
totalWidth = left;
}
totalWidth += childWidth;
getChildAt(i).setLeft(totalWidth - childWidth);
getChildAt(i).setRight(totalWidth);
getChildAt(i).setTop(childHeight * (rowCount - 1));
getChildAt(i).setBottom(childHeight * rowCount);
}
// 加上最后两个childView超过右边界换行。
if (totalWidth + editWidth + buttonWidth > right) {
rowCount++;
totalWidth = left;
}
totalWidth += editWidth + buttonWidth;
// 倒数第2个childView:收件人编辑框EditText。
getChildAt(count - 2).setLeft(totalWidth - editWidth - buttonWidth);
getChildAt(count - 2).setRight(totalWidth - buttonWidth);
getChildAt(count - 2).setTop(childHeight * (rowCount - 1));
getChildAt(count - 2).setBottom(childHeight * rowCount);
// 倒数第1个childView:addContact按钮。
getChildAt(count - 1).setLeft(right - buttonWidth);
getChildAt(count - 1).setRight(right);
getChildAt(count - 1).setTop(childHeight * (rowCount - 1));
getChildAt(count - 1).setBottom(childHeight * rowCount);
totalHeight += childHeight * rowCount;
return totalHeight;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View v = null;
for (int i = 0; i < getChildCount(); i++) {
v = getChildAt(i);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if(changed) {
layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
}
}*/
private int measureHeight(int heightMeasureSpec) {
int count = getChildCount();
int rowCount = 1;
int left = 0;
int top = 0;
int right = left + getMeasuredWidth();
int childHeight = (count == 0) ? 0 : getChildAt(0).getMeasuredHeight();
int childWidth;
int totalWidth = left;
// 最后两个child是EditText和ImageButton
int editWidth = getChildAt(count - 2).getMeasuredWidth();
int buttonWidth = getChildAt(count - 1).getMeasuredWidth();
for (int i = 0; i < count - 2; i++) {
childWidth = getChildAt(i).getMeasuredWidth();
// 加上当前childView超出右边界换行。
if (totalWidth + childWidth > right) {
rowCount++;
totalWidth = left;
}
totalWidth += childWidth;
}
// 加上最后两个childView超过右边界换行。
if (totalWidth + editWidth + buttonWidth > right) {
rowCount++;
totalWidth = left;
}
totalWidth += editWidth + buttonWidth;
return top + childHeight * rowCount;
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
int rowCount = 1;
int left = 0;
int right = left + getMeasuredWidth();
int childHeight = (count == 0) ? 0 : getChildAt(0).getMeasuredHeight();
int childWidth;
int totalWidth = left;
int totalHeight = childHeight;
// 最后两个child是EditText和ImageButton
int editWidth = getChildAt(count - 2).getMeasuredWidth();
int editHeight = getChildAt(count - 2).getMeasuredHeight();
int buttonWidth = getChildAt(count - 1).getMeasuredWidth();
View view = null;
for (int i = 0; i < count - 2; i++) {
view = getChildAt(i);
childWidth = view.getMeasuredWidth();
// 加上当前childView超出右边界换行。
if (totalWidth + childWidth > right) {
rowCount++;
totalHeight += childHeight;
totalWidth = left;
}
totalWidth += childWidth;
view.layout(totalWidth - childWidth, childHeight * (rowCount - 1),
totalWidth, childHeight * rowCount);
}
// 加上最后两个childView超过右边界换行。
if (totalWidth + editWidth + buttonWidth > right) {
rowCount++;
totalHeight += childHeight;
totalWidth = left;
}
totalWidth += editWidth + buttonWidth;
view = getChildAt(count - 2);
view.layout(totalWidth - editWidth - buttonWidth,
totalHeight - childHeight + (childHeight - editHeight)/2,
totalWidth - buttonWidth, totalHeight);
view = getChildAt(count - 1);
view.layout(right - buttonWidth, totalHeight - childHeight,
right, totalHeight);
if(changed) {
layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
}
}
public class RecipientEditor extends ViewGroup {
public RecipientEditor(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).measure(MeasureSpec.UNSPECIFIED,
MeasureSpec.UNSPECIFIED);
}
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(measuredWidth, measureHeight(heightMeasureSpec));
}
extends android.view.ViewGroup两种实现的更多相关文章
- Xamarin Android Fragment的两种加载方式
android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...
- java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.L(转)
09-09 10:19:59.979: E/AndroidRuntime(2767): FATAL EXCEPTION: main09-09 10:19:59.979: E/AndroidRuntim ...
- android环境下两种md5加密方式
在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...
- android studio gradle 两种更新方法更新
android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...
- 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)
在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...
- Android学习—下载Android SDK的两种方式
在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...
- Android视频播放的两种方式介绍
1.在Android 中播放视频的方式有两种: 第一种方式是使用MediaPlayer 结合SurfaceView 来播放,通过MediaPlayer来控制视频的播放.暂停.进度等: 通过Surfac ...
- Android 线程 thread 两种实现方法
原文链接: http://blog.csdn.net/boyupeng/article/details/6208072 这篇文章中有三点需要提前说明一下, 一: 在android中有两种实现线程thr ...
- Android—— 线程 thread 两种实现方法!(转)
原文地址:http://blog.csdn.net/boyupeng/article/details/6208072 这篇文章中有三点需要提前说明一下, 一: 在android中有两种实现线程thre ...
随机推荐
- NDK开发环境搭建_r8
本文主内容: 1. Android NDK 安装 2. 安装Cygwin与使用NDK编译 3. 在Eclipse中集成C/C++开发环境CDT 4. 安装Sequoyah插件 5. JNI编 ...
- 卷积神经网络用于视觉识别Convolutional Neural Networks for Visual Recognition
Table of Contents: Architecture Overview ConvNet Layers Convolutional Layer Pooling Layer Normalizat ...
- Linux命令缩写来由
A 命令 全称 功能 备注 apt AdvancedPackaging Tool APT用来自动下载,配置,安装二进制或者源代码格式的软件包 awk Aho Weiberger and Kerni ...
- 缓存算法:LRU、LFU、FIFO
LRU全称是Least Recently Used,即最近最久未使用的意思.如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定的空间已存满数据时,应当把最久 ...
- 【BZOJ】【3262】陌上花开
树套树 orz zyf 这题的思路……算是让我了解到了树套树的一种用途吧 三维...第一维排序,第二维树状数组,第三维treap 具体实现就是每个树状数组的节点保存一颗treap,然后就可以查询了. ...
- 【BZOJ】【3503】【CQOI2014】和谐矩阵
高斯消元解Xor方程组 Orz ZYF o(︶︿︶)o 唉我的数学太烂了…… 错误思路:对每个格点进行标号,然后根据某5个异或和为0列方程组,高斯消元找自由元……(目测N^3会TLE) ZYF的正确思 ...
- Java的多线程机制
1.利用Thread的子类创建线程 例1.用Thread子类创建多线程程序. 先定义一个Thread的子类,该类的run方法只用来输出一些信息. package thread; public clas ...
- iOS:多个单元格的删除(方法一)
采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们 ...
- Objective-C:深复制(拷贝)
深复制:复制对象时,如果对象中包含对象类型的实例变量,要对对象类型的实例变量也要做对象复制.新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是不同的对象.不管任何一方实例变量对对象做修改,都 ...
- 混沌数学之ASin模型
相关软件:混沌数学之离散点集图形DEMO 相关代码: class ASinEquation : public DiscreteEquation { public: ASinEquation() { m ...