以编程方式添加GridLayout溢出屏幕

时间:2017-09-30 10:33:31

标签: java android

我正在尝试创建一个非常基本的计算器布局(还没有逻辑)。我遇到一个非常奇怪的问题,按钮继续超过屏幕的限制。 “AC”按钮右侧还有一些神秘的空间,我无法解释。下面的图片是我在部署APK后看到的内容。

问题示例

enter image description here

这是我的代码:

public void initializeGUI()
{
    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize(size);

    int width = (size.x / COLUMN_SIZE);
    int height = (size.y / ROW_SIZE);

    GridLayout gridLayout = new GridLayout(this);
    ScrollView scrollView = new ScrollView(this);
    Button [] [] buttons = new Button[ROW_SIZE - 3] [COLUMN_SIZE];
    String [] [] buttonsTxt =
            {
                {"7","8","9","X"},
                {"4","5","6","+"},
                {"1","2","3","-"}
            };
    TextView result = setupView(new TextView(this), (width * COLUMN_SIZE), height, "#FFE4D4", Gravity.RIGHT, "0", createParams(0, 1, 0, COLUMN_SIZE));
    Button reset = setupView(new Button(this), width * 2, height, "#808000", Gravity.CENTER, "AC", createParams(1, 1, 0, 2));
    Button percent = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, "%", createParams(1, 1, 2, 1));
    Button divide = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, "/", createParams(1, 1, 3, 1));
    Button zero = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, "0", createParams(5, 1, 0, 1));
    Button equals = setupView(new Button(this), (width * 3), height, "#808000", Gravity.CENTER, "=", createParams(5, 1, 1, 3));

    gridLayout.setColumnCount(COLUMN_SIZE);
    gridLayout.setRowCount(ROW_SIZE);
    gridLayout.setBackgroundColor(Color.BLACK);

    gridLayout.addView(result);
    gridLayout.addView(reset);
    gridLayout.addView(percent);
    gridLayout.addView(divide);

    for(int row=0; row<buttons.length; row++)
    {
        for(int col=0; col<buttons[row].length; col++)
        {
            buttons [row] [col] = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, buttonsTxt[row] [col], createParams(row + 2, 1, col, 1));
            gridLayout.addView(buttons [row] [col]);
        }
    }

    gridLayout.addView(zero);
    gridLayout.addView(equals);
    scrollView.addView(gridLayout);
    setContentView(scrollView);
}

public GridLayout.LayoutParams createParams(int startingRow, int rowSize, int startingCol, int colSize)
{
    GridLayout.Spec rowSpec = GridLayout.spec(startingRow, rowSize);
    GridLayout.Spec colSpec = GridLayout.spec(startingCol, colSize);

    GridLayout.LayoutParams glp = new GridLayout.LayoutParams(rowSpec, colSpec);

    glp.topMargin = 7;
    glp.rightMargin = 7;

    return glp;
}


public Button setupView(Button b, int width, int height, String color, int gravity, String text, GridLayout.LayoutParams glp)
{
    b.setWidth(width);
    b.setHeight(height);
    b.setBackgroundColor(Color.parseColor(color));
    b.setGravity(gravity);
    b.setText(text);
    b.setTextSize(fontSize);
    b.setLayoutParams(glp);

    return b;
}

public TextView setupView(TextView tv, double width, double height, String color, int gravity, String text, GridLayout.LayoutParams glp)
{
    tv.setWidth((int)width);
    tv.setHeight((int)height);
    tv.setBackgroundColor(Color.parseColor(color));
    tv.setGravity(gravity);
    tv.setText(text);
    tv.setTextSize(fontSize);
    tv.setLayoutParams(glp);

    return tv;
}
}

1 个答案:

答案 0 :(得分:0)

您遇到麻烦的原因是因为setWidth()setHeight()没有做您要求他们做的事情(他们基本上什么都不做)。我不知道为什么会这样(同样的问题在我不知道的时候也困扰了我)。

您可以使用此代码:

,而不是使用setWidth()setHeight()
GridLayout.LayoutParams params=(GridLayout.LayoutParams)tv.getLayoutParams();
params.width=(int)width;
params.height=(int)height;
tv.setLayoutParams(params);

但是,在这种情况下使用LayoutParams仅在视图已添加时才有效,即您只应在执行setContentView(scrollView)后使用代码。您需要创建另一个小功能,您需要传递视图及其各自的宽度和高度。

这也将解决你在&#34; AC&#34;右边的额外空间的问题。按钮。这是因为GridLayout为列提供了统一的宽度(等于该列中最宽的视图),而不必担心所有视图是否占据整个宽度,并且setWidth()没有将所需宽度应用于&#34; AC&#34;按钮。