简体   繁体   English

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

[英]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. “ AC”按钮右侧还有一些我无法解释的神秘空间。 The below image is what I see once I deploy the APK. 下图是我部署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). 麻烦的原因是因为setWidth()setHeight()并未按照您要求它们执行的操作(它们基本上什么也不做)。 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: 您可以使用setWidth()来代替使用setWidth()setHeight()

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) . 但是,在这种情况下,仅在已经添加视图的情况下使用LayoutParams才有效,即,仅应在执行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. 这也将解决您的“ AC”按钮右侧的少量额外空间的问题。 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. 这是因为GridLayout为列提供了统一的宽度(等于该列中最宽的视图),而不用担心所有视图是否都占据了整个宽度,并且setWidth()并未将所需的宽度应用于“ AC”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式添加GridLayout子项的layout_columnWeight? - How to set layout_columnWeight for a GridLayout child when added programmatically? Android GridLayout-屏幕的一半 - Android GridLayout - Half of the screen 以编程方式创建ImageButton时,不会将其添加到视图中 - When creating an ImageButton programmatically it is not added to view 我的网格布局中的图像视图超出了最大宽度 - android 工作室? - Imageviews in my gridlayout are overflowing the maximum width - android studio? 将ExoPlayer添加到GLSurfaceView时获取黑屏 - Getting black screen when added ExoPlayer into GLSurfaceView 添加新项目时,ListView在屏幕上未更新 - ListView not updating on screen when new items are added Android-如何在gridlayout中以编程方式设置边距 - Android - How to set margins programmatically in gridlayout Android-以编程方式设置GridLayout中按钮之间的间距 - Android - Set Spacing between Buttons in GridLayout programmatically 如何在gridLayout中以编程方式设置列号和行号? - How to set column and row number programmatically in gridLayout? 使用RecyclerView添加到GridLayout的动态按钮彼此叠加 - Dynamic buttons added to a GridLayout using RecyclerView are added on top of each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM