在运行时在代码中设置布局属性

发布于 2024-11-03 08:33:54 字数 246 浏览 0 评论 0原文

我无法找到如何在代码中设置布局属性。 我的控件是在运行时生成的,因此我无法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-11-10 08:33:54

Mono for Android 邮件列表上的相关主题。

许多的构造函数采用 IAttributeSet 实例,因此(最坏的情况)在调用例如 RelativeLayout(Context, IAttributeSet) 构造函数

资源属性是在 Java 代码中专门处理的,因此不同类之间可能会有所不同。例如, RelativeLayout 构造函数实现

因此,属性可以(并且将会)特定于给定类型。例如,据我快速浏览 Android 源代码可知,一个类型同时具有 android:layout_alignParentBottomandroid: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,并且RelativeLayoutLinearLayout之间没有继承关系。

也就是说,要以编程方式分配 android:layout_alignParentBottom 属性,看起来您想要这样做:

// Get a RelativeLayout.LayoutParams instance
// Option 1, if you have an existing RelativeLayout instance already:
var p = (RelativeLayout.LayoutParams) layout.LayoutParameters;

// Option 2: if you don't.
var p = new RelativeLayout.LayoutParams (context, null);

// Enable layout_alignParentBottom:
p.AddRule ((int) LayoutRules.AlignParentBottom);

这使用 RelativeLayout.LayoutParams.AddRule 方法启用布局选项。 int 转换是必要的,因为我们没有意识到 AddRule() 应该采用 LayoutRules 枚举;哎呀。

以编程方式分配 android:layout_alignParentRight 属性:

p.AddRule ((int) LayoutRules.AlignParentRight);

如上所述,似乎 android:layout_weight 特定于 LinearLayout,因此我们不能使用 RelativeLayout.LayoutParams 来设置它。相反,我们需要使用 LinearLayout.LayoutParams 来设置 LinearLayout.LayoutParams.Weight 属性:

// Just like before, but get a LinearLayout.LayoutParams instance
// Option 1, if you have an existing LinearLayout instance already:
var p = (LinearLayout.LayoutParams) layout.LayoutParameters;

// Option 2: if you don't.
var p = new LinearLayout.LayoutParams (context, null);

// Enable layout_weight:
p.Weight = 1.0f;

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 and android:layout_weight attributes, as android:layout_alignParentBottom appears to be specific to the RelativeLayout type, while android:layout_weight is specific to LinearLayout, and there is no inheritance relationship between RelativeLayout and LinearLayout.

That said, to programmatically assign the android:layout_alignParentBottom property, it looks like you'd want to do:

// Get a RelativeLayout.LayoutParams instance
// Option 1, if you have an existing RelativeLayout instance already:
var p = (RelativeLayout.LayoutParams) layout.LayoutParameters;

// Option 2: if you don't.
var p = new RelativeLayout.LayoutParams (context, null);

// Enable layout_alignParentBottom:
p.AddRule ((int) LayoutRules.AlignParentBottom);

This uses the RelativeLayout.LayoutParams.AddRule method to enable the layout option. The int cast is necessary because we didn't realize that AddRule() should take a LayoutRules enum; oops.

To programmatically assign the android:layout_alignParentRight property:

p.AddRule ((int) LayoutRules.AlignParentRight);

As noted above, it appears that android:layout_weight is specific to LinearLayout, so we can't use RelativeLayout.LayoutParams to set this. Instead, we need to use LinearLayout.LayoutParams to set the LinearLayout.LayoutParams.Weight property:

// Just like before, but get a LinearLayout.LayoutParams instance
// Option 1, if you have an existing LinearLayout instance already:
var p = (LinearLayout.LayoutParams) layout.LayoutParameters;

// Option 2: if you don't.
var p = new LinearLayout.LayoutParams (context, null);

// Enable layout_weight:
p.Weight = 1.0f;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文