【发布时间】:2015-07-11 18:22:56
【问题描述】:
我有一个简单的问题。我正在扩展 ViewGroup,我想将按钮从上到下对齐到屏幕的右侧。问题是,我的屏幕上什么也没有。我已经确认这不是其他任何问题,只有我的 onLayout() 覆盖方法。你能帮帮我吗?
有问题的代码:
final int count = getChildCount();
int curWidth, curHeight, curLeft, curTop;
//get the available size of child view
int childLeft = this.getPaddingLeft();
int childTop = this.getPaddingTop();
int childRight = this.getMeasuredWidth() - this.getPaddingRight();
int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();
int childWidth = childRight - childLeft;
int childHeight = childBottom - childTop;
curTop = childTop;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
//Get the maximum size of the child
child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
curWidth = child.getMeasuredWidth();
curHeight = child.getMeasuredHeight();
child.layout(getMeasuredWidth() - getPaddingRight() - curWidth,
curTop, getMeasuredWidth() - getPaddingRight(), curTop - curHeight);
curTop -= childHeight;
}
我在我的代码中添加了一些 LOG 语句,坦率地说,我所拥有的内容令人愤怒。
07-11 14:46:46.321 32172-32172/milespeele.canvas D/Miles﹕ LEFT: 912
07-11 14:46:46.322 32172-32172/milespeele.canvas D/Miles﹕ TOP: 1008
07-11 14:46:46.322 32172-32172/milespeele.canvas D/Miles﹕ RIGHT: 1080
07-11 14:46:46.322 32172-32172/milespeele.canvas D/Miles﹕ BOTTOM: 1008
考虑到我手机屏幕的尺寸,这些都是有效的坐标(对于一个按钮),但没有出现任何按钮。
【问题讨论】: