简体   繁体   中英

GridLayout overflowing screen when added programmatically

I am attempting to create a very basic calculator layout (no logic yet). I am running into a very strange issue where the buttons are continuing on past the limits of the screen. There is also some mysterious space to the right of the "AC" button that I can't explain. The below image is what I see once I deploy the APK.

Example of problem

在此处输入图片说明

And here is my code:

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;
}
}

The reason for your trouble is because setWidth() and setHeight() are not doing what you require them to do (they are basically doing nothing). I do not know why this should be so (the same problem had troubled me too while I was unaware of it).

You can, instead of using setWidth() and setHeight() , use this:

GridLayout.LayoutParams params=(GridLayout.LayoutParams)tv.getLayoutParams();
params.width=(int)width;
params.height=(int)height;
tv.setLayoutParams(params);

However, using LayoutParams in this case only works if the view is already added, ie you should only use the code after you have executed setContentView(scrollView) . You would require creating another little function to which you need to pass your views and their respective widths and heights.

This will also solve your problem of the little extra space to the right of the "AC" button. It is being caused because GridLayout gives uniform width to the column (equal to the widest view in that column), without worrying whether all views occupy the full width, and setWidth() isn't applying the required width to the "AC" button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM