测量“确定”按钮时的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;
        }
    }

}

以上代码的输出为

enter image description here

预期输出为

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...