如何从 Java 程序调整 Android 网格布局间距

How to adjust Android gridlayout spacing from Java program

我有以下代码:

    LinearLayout symbolsTableLayout = new LinearLayout(context);
    symbolsTableLayout.setId(View.generateViewId());
    symbolsTableLayout.setBackgroundColor(Color.rgb(198,218,250));
    symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
    symbolsTableLayout.setVerticalGravity(Gravity.CENTER);

    addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,460);

    GridLayout tableLayout = new GridLayout(context);
    tableLayout.setColumnCount(col_count);
    tableLayout.setRowCount(row_count);

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.leftMargin = 0;
    layoutParams.rightMargin = 0;
    layoutParams.topMargin = 0;
    layoutParams.bottomMargin = 0;
    tableLayout.setLayoutParams(layoutParams);

    tableLayout.setBackgroundColor(Color.rgb(198,128,230));    // Color.rgb(198,128,230)   pink

    symbolsTableLayout.addView(tableLayout, 1100,470);

    symbolButton = new Button[row_count][col_count];
    for (int row = 0; row < row_count; row++)
      for (int col = 0; col < col_count; col++)
      {
        symbolButton[row][col] = new Button(context);
        symbolButton[row][col].setText(row*col_count+col+1+"");
        symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
        symbolButton[row][col].setAllCaps(false);
        symbolButton[row][col].setTextSize(4);
        symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);

        GridLayout.LayoutParams layoutParam=new GridLayout.LayoutParams();
        layoutParam.setMargins(0,0,0,0);
        layoutParam.width=50;
        layoutParam.height=58;

        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.width = 0;
        param.height = 0;

        param.setMargins(0, 0, 0, 0);
        param.columnSpec = GridLayout.spec(col, GridLayout.FILL, 0.5f);
        param.rowSpec = GridLayout.spec(row, GridLayout.FILL, 0.5f);

        symbolButton[row][col].setLayoutParams(param);
//        Symbol_Button[row][col].setLayoutParams(layoutParam);
//        Table_Layout.addView(Symbol_Button[row][col], 44, 56);
        tableLayout.addView(symbolButton[row][col], 50, 58);
      }

看起来像这样:

它只显示 22 x 8 个按钮,我想在每一行显示 25 个按钮,并使所有按钮之间的 space 变小,怎么办?

可以这样做:

    LinearLayout symbolsTableLayout = new LinearLayout(context);
    symbolsTableLayout.setId(View.generateViewId());
    symbolsTableLayout.setBackgroundColor(Color.rgb(198,218,250));
    symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
    symbolsTableLayout.setVerticalGravity(Gravity.CENTER);

    addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,460);

    GridLayout tableLayout = new GridLayout(context);
    tableLayout.setColumnCount(col_count);
    tableLayout.setRowCount(row_count);
//    tableLayout.setBackgroundColor(Color.rgb(198,128,230));    // Color.rgb(198,128,230)   pink

    symbolsTableLayout.addView(tableLayout, 1100,400);

    symbolButton = new Button[row_count][col_count];
    for (int row = 0; row < row_count; row++)
      for (int col = 0; col < col_count; col++)
      {
        symbolButton[row][col] = new Button(context);
        symbolButton[row][col].setText(row*col_count+col+1+"");
        symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
        symbolButton[row][col].setAllCaps(false);
        symbolButton[row][col].setTextSize(6);
        symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);

        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(new float[]{8, 8, 8, 8, 8, 8, 8, 8});
        drawable.setStroke(3, Color.rgb(153, 187, 255));
        drawable.setColor(Color.rgb(214, 215, 215));
        symbolButton[row][col].setBackground(drawable);

        tableLayout.addView(symbolButton[row][col], 44, 50);
      }

看起来像这样: