My Java code looks like this :
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);
}
The screen looks like this :
But I want to add more space between the first 2 rows, to look like this :
I did some search for setting space in gridlayout, but couldn't find any answer, what's the right way to do it ?
Try changing setMargins Value like below:
GridLayout.LayoutParams layoutParam=new GridLayout.LayoutParams();
layoutParam.setMargins(10,20,0,30);
Table_Layout.setLayoutParams(layoutParam);
Comment progres..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.