簡體   English   中英

在窗口小部件上以編程方式設置ImageView寬度和高度

[英]Set ImageView width and height programmatically on a widget

我正在使用下面的代碼使用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);
}

您可以通過以下方式設置布局參數:

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?

http://pastebin.com/As3L8xWu

您可以嘗試使用這些線來設置imageview的寬度和高度

image_view.getLayoutParams().height = 20;

image_view.getLayoutParams().width = 100;

這應該工作!

image.getLayoutParams().height = 90;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM