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 ...
随机推荐
- MSI failed, 不能卸载VMware
解决方法; http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&ext ...
- [Oracle] Insert All的妙用
无条件的插入 Oracle中的insert all是指把同一批数据插入到不同的表中,假如如今有个需求:把t表中的数据分别插入t1,t2,假设你不知道insert all,你可能会使用insert插入2 ...
- matlab 投影
function[l]= Gray(I) % I: The name of image A=imread(I);m=0;n=0;[m,n]= size(A);Hproj=zeros(m,1);Vpro ...
- [Android Pro] Android源码编译后,再重编译所需要做的清理工作
1.在源码目录的根目录下,make clean; 2.进到源码的\linux\kernel\目录下,执行make mrproper: 3.再退回到根目录,执行source build/envsetu ...
- 我所遭遇过的中间件--VTK
我所遭遇过的中间件--VTK Vtk是我接触的第一款软件开发包,它引导我对图形学的入门.我是先学的VTK,后学的OpenGL和D3D.VTK是专为图形学开发,特点是接口清晰,好上手,又含有大量的图像处 ...
- [16] 螺旋面(Spire)图形的生成算法
顶点数据的生成 bool YfBuildSpireVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, Yu ...
- 【反射】Reflect Class Field Method Constructor
关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...
- 直播【95秀】JNI 基本实现 简洁
2017-2-8 基本架构 1.使用SurfaceView在UI层面展示视频 2.通过JNI调用C代码控制视频的播放.停止 基本功能 1.从服务器获取正在直播的主播的列表信息 2.进入直播界面 3.可 ...
- maven 将jar 下载到工程当前目录下
在 pom.xml 的目录下,运行cmd命令 : call mvn -f pom.xml dependency:copy-dependencies 然后在同一目录下出现文件夹target,内容就是ja ...
- c#逐行分元素读取记事本txt数据写进数据库
其实这里最关键的一个方法是 StreamReader类里的 ReadLine();这个方法可以逐行读取txt流里面的数据.写了个简单的demo,已经加上了详细的注释说明. ok,好了,不废话,下面 ...