为什么 Android 中的网格布局无法将按钮与我的 java 应用正确对齐?

Why gridlayout in Android can't line up buttons properly with my java app?

我的 java 代码如下所示:

    LinearLayout Table_And_Login_Layout = new LinearLayout(context);
    Table_And_Login_Layout.setId(View.generateViewId());
    Table_And_Login_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
    Table_And_Login_Layout.setHorizontalGravity(Gravity.CENTER);
    Table_And_Login_Layout.setVerticalGravity(Gravity.CENTER);

    addView(Table_And_Login_Layout, LinearLayout.LayoutParams.MATCH_PARENT,width*row_count+13);

    GridLayout Table_Layout = new GridLayout(context);
    Table_Layout.setColumnCount(col_count);
    Table_Layout.setRowCount(row_count);
    Table_Layout.setBackgroundColor(Color.rgb(88, 188, 218));

    Table_And_Login_Layout.addView(Table_Layout, width*col_count,width*row_count+8);

    Table_Button = new Button[row_count][col_count];
    for (int row = 0; row < row_count; row++)
      for (int col = 0; col < col_count; col++)
      {
        Table_Button[row][col] = new Button(context);
        Table_Button[row][col].setTypeface(Typeface.MONOSPACE);
        Table_Button[row][col].setAllCaps(false);
        Table_Button[row][col].setTextSize(21);
        Table_Button[row][col].setOnClickListener(Table_Button_Listener);
        Table_Layout.addView(Table_Button[row][col], width, width);
//        Table_Layout.addView(Table_Button[row][col], 190, 190);

        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(new float[] { 8, 8, 8, 8, 8, 8, 8, 8 });
        drawable.setStroke(8, Color.rgb(28,158,218));
        drawable.setColor(Color.rgb(214, 215, 215));
        Table_Button[row][col].setPadding(0, 0, 0, 0);
        Table_Button[row][col].setBackground(drawable);
      }

    LinearLayout GATE_Login_Layout = new LinearLayout(context);
    GATE_Login_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
    Table_And_Login_Layout.addView(GATE_Login_Layout, width, width*2);
    GATE_Login_Layout.setHorizontalGravity(Gravity.RIGHT);
    GATE_Login_Layout.setVerticalGravity(Gravity.CENTER);

    Button Login_Button=new Button(context);
    Login_Button.setText("Login");
    Login_Button.setAllCaps(false);
    Login_Button.setOnClickListener(Login_Button_Listener);
    GATE_Login_Layout.addView(Login_Button);

...

  String getFormattedTableButton(String token)
  {
    String symbols[] = token.split(" "),text = symbols[0]+" <big>"+symbols[1]+"</big><br><small>"+symbols[2]+"</small> <big>"+symbols[3]+"</big>";
    Out(text);
    return text;
//    return "Hi";
  }

  public void resetButtons(GATE gate)
  {
    for (int row = 0; row < row_count; row++) for (int col = 0; col < col_count; col++) Table_Button[row][col].setText(Html.fromHtml(getFormattedTableButton(gate.table[row][col])));

  }

屏幕截图如下所示:

正如您所看到的,前两行按钮之间的 space 不均匀,我发现这是由按钮上的文本引起的,如果我将文本更改为 "Hi" 之类的内容,按钮会直线排列,这不是 Android 系统的错误吗?按钮应该根据网格布局排列,它们不应该因为按钮上的文字而出现锯齿状,对吗?

正文是这样的:

I/System.out: 19 <big>Ⓩ</big><br><small>♀</small> <big>%</big>
I/System.out: 2 <big>ι</big><br><small>△</small> <big>#</big>
I/System.out: 23 <big>υ</big><br><small>♥</small> <big>♫</big>
I/System.out: 9 <big>Ⓨ</big><br><small>−</small> <big>₣</big>
I/System.out: 10 <big>Ⓑ</big><br><small>♠</small> <big>€</big>
I/System.out: 41 <big>φ</big><br><small>●</small> <big>฿</big>
I/System.out: 39 <big>ξ</big><br><small>↓</small> <big>$</big>
I/System.out: 17 <big>Ⓞ</big><br><small>♂</small> <big>✉</big>
I/System.out: 27 <big>Ⓕ</big><br><small>☰</small> <big>☮</big>
I/System.out: 31 <big>σ</big><br><small>☆</small> <big>₯</big>

所以问题是"how to fix it ?"

想通了,通过为网格中的每个按钮添加另一个布局,看起来像这样:

Table_Button = new Button[row_count][col_count];
for (int row = 0; row < row_count; row++)
  for (int col = 0; col < col_count; col++)
  {
    Table_Button[row][col] = new Button(context);
    Table_Button[row][col].setTypeface(Typeface.MONOSPACE);
    Table_Button[row][col].setAllCaps(false);
    Table_Button[row][col].setTextSize(21);
    Table_Button[row][col].setGravity(Gravity.CENTER);
    Table_Button[row][col].setOnClickListener(Table_Button_Listener);

    LinearLayout Button_Layout = new LinearLayout(context);
    Button_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
    Button_Layout.setHorizontalGravity(Gravity.CENTER);
    Button_Layout.setVerticalGravity(Gravity.CENTER);
    Button_Layout.addView(Table_Button[row][col], width, width);

    Table_Layout.addView(Button_Layout, width, width);

    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setCornerRadii(new float[] { 8, 8, 8, 8, 8, 8, 8, 8 });
    drawable.setStroke(8, Color.rgb(28,158,218));
    drawable.setColor(Color.rgb(214, 215, 215));
    Table_Button[row][col].setPadding(0, 0, 0, 0);
    Table_Button[row][col].setBackground(drawable);
  }

所以现在看起来像这样: