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 ...
随机推荐
- Rete算法
RETE算法介绍一. rete概述Rete算法是一种前向规则快速匹配算法,其匹配速度与规则数目无关.Rete是拉丁文,对应英文是net,也就是网络.Rete算法通过形成一个rete网络进行模式匹配,利 ...
- 【BZOJ】【2750】【HAOI2012】Road
最短路+拓扑序DP orz zyf & lyd 统计每条边在多少条最短路径上……其实可以统计 有多少条最短路径经过了x,以及y出发到达任意一个结束点有多少种走法(沿最短路) 我们可以用Dijk ...
- Kmeans聚类算法分析(转帖)
原帖地址:http://www.opencvchina.com/thread-749-1-1.html k-means是一种聚类算法,这种算法是依赖于点的邻域来决定哪些点应该分在一个组中. ...
- TCP三次握手四次挥手相关问题探讨
TCP的握手挥手和状态转换是很多网络问题的基础.在此进行相关问题的讨论及记录. 首先,这幅图大致介绍了TCP连接和断开的过程: 注意其中的几个状态: LISTEN, SYN-SEND, SYN-RCV ...
- 测试用的数据库Transaction Log太大, 用于缩减它的脚本
记在这里, 备用. select name, recovery_model_desc from sys.databases where name = 'WSS_Content_1000' USE WS ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- SQL锁(转)
说 明 Chaos 无法改写隔离级别更高的事务中的挂起的更改. ReadCommitted 在正在读取数据时保持共享锁,以避免脏读,但是在事务结束之前可以更改数据,从而导致不可重复的读取或幻 ...
- Android -- 程序启动画面 Splash
很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐. 我的方式是使用两个Activity,程序启动时候load第一张Activity,然后由tick触发N秒钟后startActivity另外一 ...
- 最全Pycharm教程(11)——Pycharm调试器之断点篇
最全Pycharm教程(1)--定制外观 最全Pycharm教程(2)--代码风格 最全Pycharm教程(3)--代码的调试.执行 最全Pycharm教程(4)--有关Python解释器的相关配置 ...