在一个小部件上编程设置ImageView的宽度和高度

13
我正在使用以下代码在小部件imageview上设置图像,现在我想在运行时设置imageview的高度和宽度。
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(WidgetActivity1_1.this);
    RemoteViews remoteViews = new RemoteViews(WidgetActivity1_1.this.getPackageName(), R.layout.widget_layout1_1);
    Intent configIntent = new Intent(WidgetActivity1_1.this,UpdateWidgetService.class);
    configIntent.putExtra("Appid", appWidgetId);
    PendingIntent configPendingIntent = PendingIntent.getService(WidgetActivity1_1.this, appWidgetId, configIntent, 0);
    remoteViews.setImageViewResource(R.id.imgviewGrow, R.drawable.flower1);
}
3个回答

12
您可以通过以下方式以编程方式设置布局参数:
myImageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

或者设置一个数字。图片大小的操作有时可能会很棘手,建议先获取图像布局参数,进行更改,然后将其设置回去。
android.view.ViewGroup.LayoutParams layoutParams = myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);

如果您仍然无法更改其大小,请尝试将其放入LinearLayout中,并使用布局参数来操作imageView的大小:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
myImageView.setLayoutParams(layoutParams);

更新:以下帖子应该可以帮助您找到所需内容:

RemoteViews setLayoutParams?

(注:保留原文中的html标签)

http://pastebin.com/As3L8xWu


1
感谢您的回复,但这将改变ImageView的高度和宽度。我正在使用ImageView,但是在小部件中,我们无法直接访问ImageView,必须使用RemoteView。正如您在我的帖子中所看到的那样,我已经在运行时设置了图像,现在我想在RemoteView中设置高度和宽度。 - PankajAndroid
2
您正在设置ImageView布局,但在小部件中无法设置它,必须使用RemoteView。RemoteView没有一个setLayoutPrarms方法。 - PankajAndroid
我已经更新了我的答案,并提供了两个链接,这些链接对你应该有所帮助。 - Emil Adz

-5

嗨,尝试使用以下代码设置ImageView的宽度和高度

image_view.getLayoutParams().height = 20;

image_view.getLayoutParams().width = 100;

1
抱歉,请先阅读我的帖子,我要设置远程视图 ImageView 的大小,而不仅仅是布局 ImageView,请仔细阅读。 - PankajAndroid
请查看有关远程视图的文档 http://developer.android.com/reference/android/widget/RemoteViews.html#addView%28int,%20android.widget.RemoteViews%29 - androidgeek

-6

这应该可以工作!

image.getLayoutParams().height = 90;


你的回答与 AndroidGeek 的回答相比有何补充? - joaquin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接