动态添加的 RemoteView 上的布局权重

发布于 2024-12-03 16:22:02 字数 432 浏览 0 评论 0原文

在我的小部件中,我使用以下内容将项目(R.layout.widget_item)动态添加到我的主小部件布局中定义的 LinearLayout:

//--  Main widget layout
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_design);

//--  Add to main layout
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_item);
views.addView(R.id.view_container, newView);

我的问题是,如何动态设置 newView 上的“android:layout_weight”属性?

Within my widget, i'm using the following to dynamically add items (R.layout.widget_item) to a LinearLayout defined within my main widget layout:

//--  Main widget layout
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_design);

//--  Add to main layout
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_item);
views.addView(R.id.view_container, newView);

My question is, how can I dynamically set the "android:layout_weight" attribute on the newView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ˉ厌 2024-12-10 16:22:02

请参阅: http://developer.android.com/reference/android/widget/RemoteViews 您示例中的.html

newView 是 RemoteViews 类的实例,我认为您无法轻松设置 RemoteViews 提供的内容之外的任何内容。

newView.setLayoutParams(layoutParams);

上面的方法不起作用,因为它不是 RemoteViews 的一部分。

据我所知...可能有一些设置和查找RemoteViews的尺寸和其他布局参数的方法,但我还没有找到。

See: http://developer.android.com/reference/android/widget/RemoteViews.html

newView in your example is an instance of class RemoteViews and I don't think you can easily set anything above and beyond what RemoteViews provides.

newView.setLayoutParams(layoutParams);

The above will not work because it's not part of RemoteViews.

As far as I can see... there may be some round about way of setting and finding dimensions and other layoutParams of RemoteViews, but I haven't found it yet.

情绪失控 2024-12-10 16:22:02

首先你得到像这样的视图参数

layoutParams = newView.getParams();

然后编辑它
//layoutParams .setThis/setThat 各种你想要做的操作

,然后将新的参数设置回视图

newView.setLayoutParams(layoutParams);

first you get the view params like this

layoutParams = newView.getParams();

then edit tham
//layoutParams .setThis/setThat all kinds of manipulations you want to do

and then set the new params back to the view

newView.setLayoutParams(layoutParams);
束缚m 2024-12-10 16:22:02

layout_weight 是应用于 LinearLayout 的 LinearLayout.LayoutParams 的一部分,而不是 LinearLayout 本身。有一个构造函数采用代表布局权重的第三个参数。

http://developer.android.com/reference/android/widget /LinearLayout.LayoutParams.html#weight

layout_weight is part of the LinearLayout.LayoutParams that you apply to a the LinearLayout, not the LinearLayout itself. There's a constructor that takes a third param representing the layout_weight.

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight

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