如何以编程方式设置 layout_weight? [英] How to set layout_weight programmatically?
问题描述
免责声明:AFAIK 这个问题到目前为止 XAMARIN ANDROID 没有答案.Android/Java 已多次回答此问题.
Disclaimer: AFAIK This question has no answer so far for XAMARIN ANDROID. It has been answered multiple times for Android/Java.
我的问题是我没有将 layout_weight 作为参数的 GridLayoutParams 的构造函数重载...
My problem is that I have no constructor overload for GridLayoutParams that takes the layout_weight as a parameter...
以下是我尝试通过代码复制的 XML:
The following is the XML I'm trying to replicate by code:
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
不设置 layout_weight 会使控件(复选框)不可见.
Not setting layout_weight makes the control (a checkbox) simply invisible.
这是我使用 Xamarin Android 代码的位置:
Here is where I am with the Xamarin Android code:
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;
我不确定您想要行权重还是列权重,但这就是您对两者的处理方式.
I'm not sure if you want the row weight or the column weight, but that's how you do it for both.
我相信InvokeSpec()
等价于spec()
,也就是说第二个参数应该是权重,只要两个参数都是Int32
.
I believe that InvokeSpec()
is equivalent to spec()
, which means the second parameter should be the weight, as long as both parameters are Int32
.
此外,layout_weight
通常仅用于当该元素的父元素是 LinearLayout 时.您确定 CheckBox 实际上是添加到 GridLayout 而不是 LinearLayout 子项吗?
Also, layout_weight
is usually only for when the parent of that element is a LinearLayout. Are you sure that the CheckBox is actually being added to the GridLayout and not a LinearLayout child?
这篇关于如何以编程方式设置 layout_weight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!