question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

GridLayout Properties Not Properly Applied When Set In Codebehind

I have a GridLayout which contains several Button(s). All the Button(s) have a layout_gravity of center_horizontal, and the Button(s) in the first row have a column span of 2. In the Designer in Visual Studio 2019, this appears as follows:
114753-screenshot-2021-07-14-162718.png

In my code, I use the following code to rearrange the Button(s):

 for (int btnindex = 1; btnindex < 5; btnindex++)
 {
     (this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).ColumnSpec = GridLayout.InvokeSpec(btnindex - 1, 2);
     (this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).RowSpec = GridLayout.InvokeSpec(0);
 }
 for (int btnindex = 5; btnindex < 10; btnindex++)
 {
     (this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).ColumnSpec = GridLayout.InvokeSpec(btnindex - 5);
     (this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).RowSpec = GridLayout.InvokeSpec(1);
 }
 for (int btnindex = 10; btnindex < 14; btnindex++)
 {
     (this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).ColumnSpec = GridLayout.InvokeSpec(btnindex - 9);
     (this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).RowSpec = GridLayout.InvokeSpec(2);
 }
 (this.grdBidsKeypad.GetChildAt(14).LayoutParameters as GridLayout.LayoutParams).ColumnSpec = GridLayout.InvokeSpec(0);
 (this.grdBidsKeypad.GetChildAt(14).LayoutParameters as GridLayout.LayoutParams).RowSpec = GridLayout.InvokeSpec(2);
 this.grdBidsKeypad.ColumnCount = 5;
 this.grdBidsKeypad.RowCount = 3;

However, this code ends up creating the following in the emulator:
114762-screenshot-1626294503.png
It looks like either the layout_gravity is being reset or the layout_columnSpan (which, if I understand correctly, is the second parameter of InvokeSpec) is being ignored. What am I doing wrong?


dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you share the code in XAML or share a basic project to test ? You can upload it to github and attach the link here.

0 Votes 0 ·

1 Answer

njsokalski avatar image
0 Votes"
njsokalski answered

It's been a long time since I posted this question, but I finally found an answer, which hopefully can save someone else from the frustration I had. InvokeSpec requires, at least for this scenario, an alignment, such as the following:

(this.grdBidsKeypad.GetChildAt(btnindex).LayoutParameters as GridLayout.LayoutParams).ColumnSpec = GridLayout.InvokeSpec(btnindex - 1, 2, GridLayout.Center);

When I added GridLayout.Center to InvokeSpec on line 3 in my code above, it solved my problem.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.