ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-09 10:26:38  阅读:666  来源: 互联网

标签:android android-imageview android-widget


我的主屏幕小部件中有一个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 view
android.widget.ImageView can't use method with RemoteViews: setMinimumHeight(int)

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

解决方法:

使用:

    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列)

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

标签:android,android-imageview,android-widget
来源: https://codeday.me/bug/20190709/1411794.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有