如何以编程方式设置 layout_weight?
How to set layout_weight programmatically?
免责声明:AFAIK 这个问题 目前还没有 XAMARIN ANDROID 的答案。 Android/Java.
已多次回答
我的问题是我没有将 layout_weight 作为参数的 GridLayoutParams 的构造函数重载...
以下是XML我试图通过代码复制:
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
不设置 layout_weight 会使控件(复选框)完全不可见。
这是我使用 Xamarin Android 代码的地方:
CheckBox cb = new CheckBox(this);
cb.Id = View.GenerateViewId();
GridLayout.LayoutParams cbButtonLayoutParams = new GridLayout.LayoutParams();
cbButtonLayoutParams.RowSpec = GridLayout.InvokeSpec(row, 1); // Setting row and rowspan respectivly
cbButtonLayoutParams.ColumnSpec = GridLayout.InvokeSpec(column, 1); // Setting col and colspan respectivley
cbButtonLayoutParams.Width = 0;
cbButtonLayoutParams.Height = GridLayout.LayoutParams.WrapContent;
cbButtonLayoutParams.SetGravity(GravityFlags.Center);
// How do I set the weight ? There is no property nor constructor...
// cbButtonLayoutParams.???
cb.LayoutParameters = cbButtonLayoutParams;
感谢任何帮助。
在各自的Spec
下。
cbButtonLayoutParams.RowSpec.weight = 1; //might be Weight in C#
cbButtonLayoutParams.ColumnSpec.weight = 1;
我不确定您是想要行宽还是列宽,但两者都是这样做的。
编辑:
我认为InvokeSpec()
等同于spec()
,也就是说第二个参数应该是权重,只要两个参数都是Int32
.
此外,layout_weight
通常只适用于该元素的父元素是 LinearLayout 的情况。您确定 CheckBox 实际上被添加到 GridLayout 而不是 LinearLayout 子项吗?
免责声明:AFAIK 这个问题 目前还没有 XAMARIN ANDROID 的答案。 Android/Java.
已多次回答我的问题是我没有将 layout_weight 作为参数的 GridLayoutParams 的构造函数重载...
以下是XML我试图通过代码复制:
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
不设置 layout_weight 会使控件(复选框)完全不可见。
这是我使用 Xamarin Android 代码的地方:
CheckBox cb = new CheckBox(this);
cb.Id = View.GenerateViewId();
GridLayout.LayoutParams cbButtonLayoutParams = new GridLayout.LayoutParams();
cbButtonLayoutParams.RowSpec = GridLayout.InvokeSpec(row, 1); // Setting row and rowspan respectivly
cbButtonLayoutParams.ColumnSpec = GridLayout.InvokeSpec(column, 1); // Setting col and colspan respectivley
cbButtonLayoutParams.Width = 0;
cbButtonLayoutParams.Height = GridLayout.LayoutParams.WrapContent;
cbButtonLayoutParams.SetGravity(GravityFlags.Center);
// How do I set the weight ? There is no property nor constructor...
// cbButtonLayoutParams.???
cb.LayoutParameters = cbButtonLayoutParams;
感谢任何帮助。
在各自的Spec
下。
cbButtonLayoutParams.RowSpec.weight = 1; //might be Weight in C#
cbButtonLayoutParams.ColumnSpec.weight = 1;
我不确定您是想要行宽还是列宽,但两者都是这样做的。
编辑:
我认为InvokeSpec()
等同于spec()
,也就是说第二个参数应该是权重,只要两个参数都是Int32
.
此外,layout_weight
通常只适用于该元素的父元素是 LinearLayout 的情况。您确定 CheckBox 实际上被添加到 GridLayout 而不是 LinearLayout 子项吗?