动态添加的 RemoteView 上的布局权重
在我的小部件中,我使用以下内容将项目(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅: http://developer.android.com/reference/android/widget/RemoteViews 您示例中的.html
newView 是 RemoteViews 类的实例,我认为您无法轻松设置 RemoteViews 提供的内容之外的任何内容。
上面的方法不起作用,因为它不是 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.
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.
首先你得到像这样的视图参数
然后编辑它
//layoutParams .setThis/setThat 各种你想要做的操作
,然后将新的参数设置回视图
first you get the view params like this
then edit tham
//layoutParams .setThis/setThat all kinds of manipulations you want to do
and then set the new params back to the view
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