如何将 GridLayout.Spec 用于扩展多行的列?
How to use GridLayout.Spec for a column extending multiple rows?
我正在尝试动态创建具有 2 行和 4 列的 table。但是每种情况下行和列的大小不应该相同。也许我会附上一张图片:
我愿意:
据我所知,当我不使用 XML 标记时,我会使用 setLayoutParamsGridLayout.Spec 中的 GridLayout.Spec 来实现此目的
但是我不明白我需要传入什么参数GridLayout.Spec?
如果我想把第一行的第一列和第二行的第一列合并,我应该在
中指定什么
GridLayout.LayoutParams (GridLayout.Spec rowSpec, GridLayout.Spec columnSpec)
?
什么是规格?
我看到了示例 there and there 但不完全理解它是如何工作的。
如果在 XML 标记的情况下,我可以使用 layout_rowSpan 和 layout_columnSpan 合并行或列,那么如何动态地合并?
查看下面的代码以获得您正在寻找的布局。添加注释以解释 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
。
我正在尝试动态创建具有 2 行和 4 列的 table。但是每种情况下行和列的大小不应该相同。也许我会附上一张图片:
我愿意:
据我所知,当我不使用 XML 标记时,我会使用 setLayoutParamsGridLayout.Spec 中的 GridLayout.Spec 来实现此目的
但是我不明白我需要传入什么参数GridLayout.Spec?
如果我想把第一行的第一列和第二行的第一列合并,我应该在
中指定什么GridLayout.LayoutParams (GridLayout.Spec rowSpec, GridLayout.Spec columnSpec)
?
什么是规格?
我看到了示例 there and there 但不完全理解它是如何工作的。
如果在 XML 标记的情况下,我可以使用 layout_rowSpan 和 layout_columnSpan 合并行或列,那么如何动态地合并?
查看下面的代码以获得您正在寻找的布局。添加注释以解释 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
。