Java 不明白如何使用 GridLayout.spec()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20871690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:54:32  来源:igfitidea点击:

Don't understand how to use GridLayout.spec()

javaandroidgrid-layoutspecifications

提问by Matt

This GridLayout is going in my app that has a lot of levels. Each level has a different number of rows and columns. I assume that this GridLayout would be my best bet to use to satisfy my needs. Also, all need to be done at runtime prorammatically.

这个 GridLayout 在我的应用程序中有很多级别。每个级别都有不同数量的行和列。我认为这个 GridLayout 将是我用来满足我的需求的最佳选择。此外,所有这些都需要在运行时以编程方式完成。

I am having trouble understanding how to use GridLayout.spec(). I am trying to follow this excellent examplebut just cannot grasp it fully. Let's say, for example, I want a GridLayout with 3 columns and 4 rows.

我在理解如何使用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);

In my linked example above, he used code like below to set the "specs".

在我上面链接的示例中,他使用如下代码来设置"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);

I don't understand the parameters of those variables either. I've tried reading the documentation but it didn't give me any clarity. Can someone help me with my example code of the 3x4 GridLayout which also helps explain what the Specs are?

我也不明白这些变量的参数。我试过阅读文档,但它没有给我任何清晰度。有人可以帮助我使用 3x4 GridLayout 的示例代码,这也有助于解释Specs 是什么吗?

采纳答案by tereru

I didn't exactly understand your question, but here are some examples that explain the syntax:

我没有完全理解你的问题,但这里有一些解释语法的例子:

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) 

and etc.

等等。

Spec col0 = GridLayout.spec(0); //same here - first column, width = 1 cell.

Spec colspan2 = GridLayout.spec(0, 2);

so you can do like this:

所以你可以这样做:

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.

I hope this helps. :)

我希望这有帮助。:)