在我的主屏幕小部件中有一个ImageView
。在布局文件中,我将高度和宽度设置为wrap_content
。但根据用户的输入,我必须将其高度和宽度更改为match_parent
。怎么能做到?
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
现在我尝试对这个对象使用
setInt()
方法来检查是否可以更改高度和宽度:rv.setInt(R.id.widgetImageView, "setMinimumHeight", 300);
但这就是我对Logcat的看法:
couldn't find any view, using error view
android.widget.ImageView can't use method with RemoteViews: setMinimumHeight(int)
那么如何将它的高度和宽度更改为
RemoteViews
? 最佳答案
用途:
ImageView view = new ImageView();
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
LayoutParams
的第一个参数表示宽度,第二个参数表示高度。在这里,我创建了一个新的imageview来解释。您应该在代码中使用自己的imageview变量来执行上述操作。我希望这是清楚的。
[代码更新]:对不起,
MATCH_PARENT
变量在LayoutParams
下,而不是View
下。我已经更新了代码。[更新]:以上答案不适用于
RemoteViews
,因为RemoteViews
不能有布局参数。基于此SO Question中给出的答案,无法通过常规方法设置或查找远程视图的维度。我发现这个Android Widget Tutorial,在widget size部分下,作者说:
A Widget will take a certain amount of cells on the homescreen. A cell is
usually used to display the icon of one application. As a calculation rule
you should define the size of the widget with the formula:
((Number of columns / rows)* 74) - 2.
These are device independent pixels and the -2 is used to avoid rounding issues.
As of Android 3.1 a Widgets can be flexible in size, e.g. the user can make it
larger or smaller. To enable this for Widgets you can use the
android:resizeMode="horizontal|vertical" attribute in the XML configuration file
for the widget.
因此,我可以建议您,与其直接设置特定的整数大小值,不如设置要调整大小的行数和列数?例如,如果初始窗口小部件大小为
4 Columns and 1 Row
,则在用户输入时,可以将其更改为4 Columns and 4 Rows
,从而使其占据整个屏幕(窗口小部件的最大大小为4行4列)我知道上面的方法不是你想要的,但这似乎是我唯一的方法。试试看是否有用。