我的Java代码如下所示:

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

h = (int)Math.ceil(0.5375 * MainActivity.h);
addView(Table_And_Login_Layout, LinearLayout.LayoutParams.MATCH_PARENT,h);

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

GridLayout.LayoutParams layoutParam=new GridLayout.LayoutParams();
layoutParam.setMargins(10,20,0,10);
Table_Layout.setLayoutParams(layoutParam);

w = (int)Math.ceil(0.82 * MainActivity.w);
h = (int)Math.ceil(0.53611111 * MainActivity.h);
Table_And_Login_Layout.addView(Table_Layout, w,h);

Table_Button = new Button[row_count][col_count];
Table_Button_Layout = new LinearLayout[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);

    Table_Button_Layout[row][col] = new LinearLayout(context);
    Table_Button_Layout[row][col].setBackgroundColor(adjustColor(itemColor[selectedColorIndex],1));
    Table_Button_Layout[row][col].setHorizontalGravity(Gravity.CENTER);
    Table_Button_Layout[row][col].setVerticalGravity(Gravity.CENTER);
    Table_Button_Layout[row][col].addView(Table_Button[row][col], buttonW, buttonH);
    Table_Layout.addView(Table_Button_Layout[row][col], buttonW, buttonH);

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

屏幕看起来像这样:
java - 如何使用Java在Android GridLayout中设置间距?-LMLPHP
但我想在前两行之间添加更多空间,如下所示:
java - 如何使用Java在Android GridLayout中设置间距?-LMLPHP
我进行了一些搜索,以在gridlayout中设置空间,但是找不到任何答案,正确的方法是什么?

最佳答案

尝试如下更改setMargins值:

GridLayout.LayoutParams layoutParam=new GridLayout.LayoutParams();
layoutParam.setMargins(10,20,0,30);
Table_Layout.setLayoutParams(layoutParam);


评论进度

07-26 03:48