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 ...
随机推荐
- 修改后无警告全面支持non-ARC以及ARC的OpenUDID
OpenUDID Open source initiative for a universal and persistent UDID solution for iOS. 首创的给iOS提供设备唯一标 ...
- android之获得当前连接wifi的名字
WifiManager wifiMgr = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE); int wifiState ...
- MySQL命令行查询结果中文显示乱码
数据库编码格式为utf8,表和字段也都是utf8,存进去的格式是utf-8 但是用命令行工具查询命令select * from 表名; 查询出来的中文是乱码 原因:MySQL客户端根本就不能以utf8 ...
- C/C++字符串查找函数
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- Linux C Socket编程原理及简单实例
部分转自:http://goodcandle.cnblogs.com/archive/2005/12/10/294652.aspx 1. 什么是TCP/IP.UDP? 2. Socket在哪里 ...
- Cesium随笔(2)加载天地图地图服务【转】
Cesium自带的图层是bing地图和esri,mapbox等图层,木有中文注记,想加载中文注记的图层?废话不说,上代码: var viewer = new Cesium.Viewer('cesium ...
- 不要再使用JS框架了
停止编写Javascript框架吧. Javascript框架就好像死亡和税收一样:终究不可避免它的存在.我确信如果我是那面墙上的一只苍蝇,每次有人开始一个新的网页项目时,第一个问题肯定是我们用的是哪 ...
- GO语言基础条件、跳转、Array和Slice
1. 判断语句if 1. 条件表达式没有括号(这点其他语言转过来的需要注意) 2. 支持一个初始化表达式(可以是并行方式,即:a, b, c := 1, 2, 3) 3. 左大括号必须和条件语句或 e ...
- CommonCode升级:把不常用的Sqlite独立出去
CommonCode大概一年多没有更新了,今天碰到一件闹心的事情,结果用一行代码解决了京东购物车信息提取工具.一不小心,把一贯以来的一个念头又给惹起来了:就是把程序做成又给独立的exe,不要这么多dl ...
- 创建并发布node.js module
创建node.js module. 创建一个文件夹,用来存放module. Cd到新创建的文件夹,运行npm init,会提示输入package的信息. 可以按照这个视频的来输入.Test com ...