我试图动态创建一个包含2行和4列的表。但是每种情况下行和列的大小不应该相同。也许我会附上一张图片:
我想:
据我所知,当我不使用XML标记来实现这一点时,使用setLayoutParams中的GridLayout.Spec
但我不明白我需要在GridLayout.Spec中传递哪些参数?
如果我想将第一行中的第一列与第二行的第一列合并,我应该在
中指定什么GridLayout.LayoutParams (GridLayout.Spec rowSpec, GridLayout.Spec columnSpec)
什么是Spec?
我看到了示例there和there,但没有完全理解它是如何工作的。
如果是XML标记,我可以使用layout_rowSpan和layout_columnSpan来合并行或列,然后如何动态地进行操作?
答案 0 :(得分:2)
请查看下面的代码,了解您正在寻找的布局。添加注释以解释GridLayout.Spec
创建中使用的值。
// simple grid layout with WRAP_CONTENT width and height
GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);
// face view takes 2 rows, 1 column -- zero index based
GridLayout.Spec faceRow = GridLayout.spec(0, 2); // starts row 0, takes 2 rows
GridLayout.Spec faceCol = GridLayout.spec(0); // starts col 0, takes 1 col
GridLayout.Spec titleRow = GridLayout.spec(0); // starts row 0, takes 1 row
GridLayout.Spec titleCol = GridLayout.spec(1, 3); // starts col 1, takes 3 cols
GridLayout.Spec plusRow = GridLayout.spec(1); // starts row 1, takes 1 row
GridLayout.Spec plusCol = GridLayout.spec(1); // starts col 1, takes 1 col
GridLayout.Spec minusRow = GridLayout.spec(1); // starts row 1, takes 1 row
GridLayout.Spec minusCol = GridLayout.spec(2); // starts col 1, takes 1 col
GridLayout.Spec checkRow = GridLayout.spec(1); // starts row 1, takes 1 row
GridLayout.Spec checkCol = GridLayout.spec(3); // starts col 1, takes 1 col
// create the LayoutParams using our row/col for each view
GridLayout.LayoutParams faceParams = new GridLayout.LayoutParams(faceRow, faceCol);
faceParams.setGravity(Gravity.FILL_VERTICAL); // fill vertical so we take up the full 2 rows
// dummy text views to fill some space
TextView faceText = new TextView(this);
faceText.setPadding(32, 32, 32, 32); // add some random padding to make the views bigger
faceText.setLayoutParams(faceParams);
faceText.setText("FACE");
faceText.setGravity(Gravity.CENTER);
faceText.setBackgroundColor(Color.RED);
gridLayout.addView(faceText, faceParams);
GridLayout.LayoutParams titleParams = new GridLayout.LayoutParams(titleRow, titleCol);
titleParams.setGravity(Gravity.FILL_HORIZONTAL); // fill horizontal so we take up the full 3 columns
TextView titleText = new TextView(this);
titleText.setPadding(32, 32, 32, 32);
titleText.setLayoutParams(titleParams);
titleText.setText("TITLE");
titleText.setGravity(Gravity.CENTER);
titleText.setBackgroundColor(Color.BLUE);
gridLayout.addView(titleText, titleParams);
GridLayout.LayoutParams minusParams = new GridLayout.LayoutParams(minusRow, minusCol);
TextView minusText = new TextView(this);
minusText.setPadding(32, 32, 32, 32);
minusText.setLayoutParams(minusParams);
minusText.setText("MIN");
minusText.setGravity(Gravity.CENTER);
minusText.setBackgroundColor(Color.YELLOW);
gridLayout.addView(minusText, minusParams);
GridLayout.LayoutParams plusParams = new GridLayout.LayoutParams(plusRow, plusCol);
TextView plusText = new TextView(this);
plusText.setPadding(32, 32, 32, 32);
plusText.setLayoutParams(plusParams);
plusText.setText("PLS");
plusText.setGravity(Gravity.CENTER);
plusText.setBackgroundColor(Color.GREEN);
gridLayout.addView(plusText, plusParams);
GridLayout.LayoutParams checkParams = new GridLayout.LayoutParams(checkRow, checkCol);
TextView checkText = new TextView(this);
checkText.setPadding(32, 32, 32, 32);
checkText.setLayoutParams(faceParams);
checkText.setText("CHK");
checkText.setGravity(Gravity.CENTER);
checkText.setBackgroundColor(Color.MAGENTA);
gridLayout.addView(checkText, checkParams);
希望这有助于理解GridLayout.Spec
。