在运行时在代码中设置布局属性
我无法找到如何在代码中设置布局属性。 我的控件是在运行时生成的,因此我无法在 xml 中设置布局。
我希望能够设置
android:layout_alignParentBottom="true" 等属性 机器人:layout_alignParentRight =“真” android:layout_weight="1"
但是我找不到任何有关如何在代码中执行此操作的文档或示例。 当前版本的 Android 版 Mono 可以吗?
I am having trouble finding out how to set layout properties in my code.
My controls are being generated at runtime so I can't set the layout in my xml.
I would like to be able to set properties such as
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_weight="1"
However I can't find any documentation or examples on how to do this in code.
Is it possible with the current version of Mono for Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mono for Android 邮件列表上的相关主题。
许多的构造函数采用 IAttributeSet 实例,因此(最坏的情况)在调用例如 RelativeLayout(Context, IAttributeSet) 构造函数。
资源属性是在 Java 代码中专门处理的,因此不同类之间可能会有所不同。例如, RelativeLayout 构造函数实现。
因此,属性可以(并且将会)特定于给定类型。例如,据我快速浏览 Android 源代码可知,一个类型同时具有
android:layout_alignParentBottom
和android:layout_weight
属性是无效的,如 < a href="http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignParentBottom" rel="noreferrer">android:layout_alignParentBottom 似乎特定于 < code>RelativeLayout 类型,而 android:layout_weight 特定于 LinearLayout,并且RelativeLayout
和LinearLayout
之间没有继承关系。也就是说,要以编程方式分配
android:layout_alignParentBottom
属性,看起来您想要这样做:这使用 RelativeLayout.LayoutParams.AddRule 方法启用布局选项。
int
转换是必要的,因为我们没有意识到AddRule()
应该采用LayoutRules
枚举;哎呀。以编程方式分配
android:layout_alignParentRight
属性:如上所述,似乎 android:layout_weight 特定于
LinearLayout
,因此我们不能使用RelativeLayout.LayoutParams
来设置它。相反,我们需要使用 LinearLayout.LayoutParams 来设置 LinearLayout.LayoutParams.Weight 属性:Relevant thread on the Mono for Android mailing list.
Many of the constructors take an IAttributeSet instance, so (worst case) you could always provide the XML custom attributes through that parameter when invoking the e.g. RelativeLayout(Context, IAttributeSet) constructor.
Resource attributes are handled specifically in Java code, and thus can potentially vary from one class to another. For example, the RelativeLayout constructor implementation.
Because of this, attributes can (and will be) specific to a given type. For example, as best as I can tell from quickly perusing the Android source, it's not valid for a type to have both
android:layout_alignParentBottom
andandroid:layout_weight
attributes, as android:layout_alignParentBottom appears to be specific to theRelativeLayout
type, while android:layout_weight is specific to LinearLayout, and there is no inheritance relationship betweenRelativeLayout
andLinearLayout
.That said, to programmatically assign the
android:layout_alignParentBottom
property, it looks like you'd want to do:This uses the RelativeLayout.LayoutParams.AddRule method to enable the layout option. The
int
cast is necessary because we didn't realize thatAddRule()
should take aLayoutRules
enum; oops.To programmatically assign the
android:layout_alignParentRight
property:As noted above, it appears that android:layout_weight is specific to
LinearLayout
, so we can't useRelativeLayout.LayoutParams
to set this. Instead, we need to useLinearLayout.LayoutParams
to set the LinearLayout.LayoutParams.Weight property: