13

我正在使用下面的代码使用 remoteview 在小部件 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);
}
4

3 回答 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 设置布局参数?

http://pastebin.com/As3L8xWu

于 2013-03-02T06:58:28.317 回答
-5

嗨,试试这些线来设置 imageview 的宽度和高度

image_view.getLayoutParams().height = 20;

image_view.getLayoutParams().width = 100;
于 2013-03-02T07:16:07.497 回答
-6

这应该工作!

image.getLayoutParams().height = 90;

于 2013-12-31T09:16:00.530 回答