簡體   English   中英

以編程方式添加時GridLayout溢出屏幕

[英]GridLayout overflowing screen when added programmatically

我正在嘗試創建一個非常基本的計算器布局(尚無邏輯)。 我遇到了一個非常奇怪的問題,其中按鈕繼續超出屏幕的限制。 “ AC”按鈕右側還有一些我無法解釋的神秘空間。 下圖是我部署APK后看到的圖像。

問題的例子

在此處輸入圖片說明

這是我的代碼:

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

麻煩的原因是因為setWidth()setHeight()並未按照您要求它們執行的操作(它們基本上什么也不做)。 我不知道為什么會這樣(當我不知道時,同樣的問題也困擾着我)。

您可以使用setWidth()來代替使用setWidth()setHeight()

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

但是,在這種情況下,僅在已經添加視圖的情況下使用LayoutParams才有效,即,僅應在執行setContentView(scrollView)之后使用代碼。 您將需要創建另一個小功能,您需要將視圖及其各自的寬度和高度傳遞給該函數。

這也將解決您的“ AC”按鈕右側的少量額外空間的問題。 這是因為GridLayout為列提供了統一的寬度(等於該列中最寬的視圖),而不用擔心所有視圖是否都占據了整個寬度,並且setWidth()並未將所需的寬度應用於“ AC”按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM