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.
I am having trouble understanding how to use GridLayout.spec()
. I am trying to follow this excellent example but just cannot grasp it fully. Let's say, for example, I want a GridLayout with 3 columns and 4 rows.
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"
.
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 Spec
s are?
android.widget.GridLayout.Spec. A Spec defines the horizontal or vertical characteristics of a group of cells. Each spec. defines the grid indices and alignment along the appropriate axis. The grid indices are the leading and trailing edges of this cell group.
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
android.widget.GridLayout. A layout that places its children in a rectangular grid. The grid is composed of a set of infinitely thin lines that separate the viewing area into cells. Throughout the API, grid lines are referenced by grid indices.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With