I tried using the following bit of code, but to no avail.
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.Width = ViewGroup.LayoutParams.WrapContent;
layoutParams.Height = ViewGroup.LayoutParams.WrapContent;
layoutParams.ColumnSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
layoutParams.RowSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
For reference, this is the grid layout under which it's added
<android.support.v7.widget.GridLayout
android:layout_below="@+id/lblNaslovRate"
android:id="@+id/GridRate"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:columnCount="3"
app:rowCount="5"
android:paddingLeft="20dp"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="0.0dp">
layout_columnWeight
and layout_rowWeight
. These two properties are attached to the children of GirdLayout. Just like this:
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_below="@+id/lblNaslovRate"
android:id="@+id/GridRate"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="2"
android:paddingLeft="20dp"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="0.0dp">
<TextView android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="0"
app:layout_row="0"
android:text="Cell 0"/>
....
</android.support.v7.widget.GridLayout>
If you want to set them programmatically, you can add this to your code:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var gl = FindViewById<GridLayout>(Resource.Id.GridRate);
for (int i = 0; i < gl.RowCount; i++)
{
GridLayout.Spec rowSpec = GridLayout.InvokeSpec(i, 1f);
for (int j = 0; j < gl.ColumnCount; j++)
{
GridLayout.Spec colSpec = GridLayout.InvokeSpec(j, 1f);
TextView tvChild = new TextView(this);
tvChild.Text = "111";
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
myGLP.RowSpec = rowSpec;
myGLP.ColumnSpec = colSpec;
gl.AddView(tvChild, myGLP);
}
}
}
For more info you can refer to GridLayout Spec.
Update
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_below="@+id/lblNaslovRate"
android:id="@+id/GridRate"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="2"
android:paddingLeft="20dp"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="0.0dp">
</android.support.v7.widget.GridLayout>
Remember to add the NuGet package and search for Xamarin.Android.Support.v7.GridLayout
. This is the effect.
Second Update
According to your layout, you can see this summary.