不了解如何使用GridLayout.spec()
问题内容:
此GridLayout将在我的具有很多级别的应用程序中使用。每个级别具有不同数量的行和列。我认为,最好使用GridLayout满足我的需求。同样,所有这些都需要在运行时按比例完成。
我在理解如何使用时遇到了麻烦GridLayout.spec()
。我正在尝试遵循这个出色的例子,但无法完全掌握它。比方说,例如,我想要一个3列4行的GridLayout。
GridLayout.LayoutParms params1 = new GridLayout.Layout(rowSpec, columnSpec); //what's parameters?
gameplayGridLayout.setColumnCount(3);
gameplayGridLayout.setRowCount(4);
puzzle.addView(gameplayGridLayout, params1);
在上面的链接示例中,他使用了如下代码来设置"specs"
。
Spec row1 = GridLayout.spec(0, 2);
Spec row2 = GridLayout.spec(2);
Spec row3 = GridLayout.spec(3);
Spec row4 = GridLayout.spec(4, 2);
Spec col0 = GridLayout.spec(0);
Spec col1 = GridLayout.spec(1);
Spec colspan2 = GridLayout.spec(0, 2);
我也不理解这些变量的参数。我尝试阅读文档,但并没有给我任何清晰的信息。有人可以帮我提供3x4
GridLayout的示例代码,它也有助于解释Spec
s是什么吗?
问题答案:
我不太了解您的问题,但以下是一些解释语法的示例:
Spec row1 = GridLayout.spec(0, 2); //here you set row to be first row and it takes 2 cells in height.
Spec row2 = GridLayout.spec(2); //this row goes under row1 and it takes 1 cell(default size = 1)
等等。
Spec col0 = GridLayout.spec(0); //same here - first column, width = 1 cell.
Spec colspan2 = GridLayout.spec(0, 2);
因此您可以这样做:
Spec row1 = GridLayout.spec(0);
Spec row2 = GridLayout.spec(1);
Spec row3 = GridLayout.spec(2);
Spec row4 = GridLayout.spec(3);
Spec col0 = GridLayout.spec(0);
Spec col1 = GridLayout.spec(1);
Spec col2 = GridLayout.spec(2);
GridLayout gridLayout = new GridLayout(this);
GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, col0);
/*Here you can set options for first cell which is in first row and first column.*/
first.width = screenWidth;
first.height = quarterScreenWidth * 2;
twoByTwo1.setLayoutParams(first);
twoByTwo1.setGravity(Gravity.CENTER);
twoByTwo1.setBackgroundColor(Color.RED);
twoByTwo1.setText("TOP");
twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByTwo1, first)
//You can set all cells like above.
我希望这有帮助。:)