如何将Android小部件中ImageView的高度和宽度动态设置为“match_parent”?

如何将Android小部件中ImageView的高度和宽度动态设置为“match_parent”?,第1张

概述我的主屏幕小部件中有一个 ImageView.在布局文件中,我将高度宽度设置为wrap_content.但是根据用户的输入,我必须将其高度和宽度更改为match_parent.如何才能做到这一点 ? RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 现在我尝试在这个RemoteV @H_403_4@ 我的主屏幕小部件中有一个 ImageVIEw.在布局文件中,我将高度和宽度设置为wrap_content.但是根据用户的输入,我必须将其高度和宽度更改为match_parent.如何才能做到这一点 ?

RemoteVIEws rv = new RemoteVIEws(context.getPackagename(),R.layout.Widget_layout);

现在我尝试在这个RemoteVIEws对象上使用setInt()方法来检查是否可以更改高度和宽度:

rv.setInt(R.ID.wIDgetimageVIEw,"setMinimumHeight",300);

但这是我在logcat上得到的:

Couldn't find any vIEw,using error vIEwandroID.Widget.ImageVIEw can't use method with RemoteVIEws: setMinimumHeight(int)

那么如何将其高度和宽度更改为match_parent?

@H_403_4@解决方法 使用:

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中给出的答案,无法通过常规方法设置或查找RemoteVIEw的维度.

我发现这是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列和1行,那么在用户输入上,您可以将其更改为4列和4行,从而使其占据整个屏幕(窗口小部件的最大大小为4行和4列)

我知道上面的方法不是你想要的,但这对我来说似乎是唯一的方法.试着看看它是否有用.

@H_403_4@ @H_403_4@ @H_403_4@ @H_403_4@ 总结

以上是内存溢出为你收集整理的如何将Android小部件中ImageView的高度和宽度动态设置为“match_parent”?全部内容,希望文章能够帮你解决如何将Android小部件中ImageView的高度和宽度动态设置为“match_parent”?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1124821.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存