如何解决测量“确定”按钮时的ViewGroup onLayout问题
我通过扩展了ViewGroup 创建了键盘。我在 onLayout()方法上遇到问题。我要将确定按钮从一个正方形更改为三个正方形。怎么做?
public class KeyboardVolume extends ViewGroup {
@Override
protected void onLayout(boolean c,int l,int t,int r,int b) {
int height = b - t; // view height = view bottom - view top
int width = r - l; // view width = view right - view left
int rows = 4; // rows count (4 keys)
int colums = 4; // columns count (4 keys)
int gridW = (width / colums); // key width
int gridH = (height / rows); // key height
int left;
int top = 1;
int count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < colums; j++) {
count++;
int id = (i * colums) + j;
View child = this.getChildAt(id);
if (child == null) {
return;
}
left = j * (gridW + 1);
if (gridW != child.getMeasuredWidth() || gridH != child.getMeasuredHeight()) {
child.measure(makeMeasureSpec(gridW,EXACTLY),makeMeasureSpec(gridH,EXACTLY));
}
if (count < (rows * colums)) {
child.layout(left,top,left + gridW,top + gridH);
} else {
left -= 1;
top -= 1;
child.layout(left,top + gridH);
}
}
top += gridH + 1;
}
}
}
以上代码的输出为
预期输出为
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。