使用:
ImageView view = new ImageView();
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
LayoutParams
的第一个参数是宽度,第二个参数是高度。
在这里,我为了解释创建了一个新的ImageView。你应该使用自己的代码中的ImageView变量来执行上述操作。我希望这很清楚。
[更新代码]: 对不起,MATCH_PARENT
变量在View
下而不是LayoutParams
下。我已经更新了代码。
[更新]: 上面的答案对于RemoteViews
不起作用,因为RemoteViews
不能有布局参数。根据SO问题中给出的答案,没有办法通过常规方法设置或查找远程视图的尺寸。
我发现了Android Widget Tutorial,其中作者在“小部件大小”部分中提到:
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列1行
,那么在用户输入后,您可以将其更改为
4列4行
,从而使其占据整个屏幕(小部件的最大尺寸为4行4列)。
我知道上述方法可能不是您想要的,但在我看来,这似乎是唯一的方法。请尝试并查看是否有帮助。
RemoteViews
中视图的可见性:rv.setViewVisibility(R.id.widgetImageView1, View.GONE)
。 - Stanislav