天天看點

android imageview 設定圖檔高度,android - 以程式設計方式設定ImageView的寬度和高度?

android - 以程式設計方式設定ImageView的寬度和高度?

如何以程式設計方式設定ImageView的寬度和高度?

9個解決方案

949 votes

可能為時已晚,但為了遇到同樣問題的其他人,設定ImageView的高度:

image_view.getLayoutParams().height = 20;

希望這可以幫助。

重要。 如果您在布局已經“布局”後設定高度,請確定同時調用:

image_view.requestLayout()

Hakem Zaied answered 2019-01-17T05:53:30Z

205 votes

如果您的圖像視圖是動态的,則包含getLayout的答案将失敗并顯示null異常。

在這種情況下,正确的方法是:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);

iv.setLayoutParams(layoutParams);

InvulgoSoft answered 2019-01-17T05:53:59Z

44 votes

這個簡單的方法來完成你的任務:

setContentView(R.id.main);

ImageView iv = (ImageView) findViewById(R.id.left);

int width = 60;

int height = 60;

LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);

iv.setLayoutParams(parms);

另一種方法,如果你想給出高度和寬度的螢幕尺寸,那麼使用下面的代碼:

setContentView(R.id.main);

Display display = getWindowManager().getDefaultDisplay();

ImageView iv = (LinearLayout) findViewById(R.id.left);

int width = display.getWidth(); // ((display.getWidth()*20)/100)

int height = display.getHeight();// ((display.getHeight()*30)/100)

LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);

iv.setLayoutParams(parms);

希望充分利用你。

duggu answered 2019-01-17T05:54:51Z

28 votes

我用pixel和dp玩過這個。

private int dimensionInPixel = 200;

如何按像素設定:

view.getLayoutParams().height = dimensionInPixel;

view.getLayoutParams().width = dimensionInPixel;

view.requestLayout();

如何通過dp設定:

int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());

view.getLayoutParams().height = dimensionInDp;

view.getLayoutParams().width = dimensionInDp;

view.requestLayout();

希望這會對你有所幫助。

Hiren Patel answered 2019-01-17T05:55:38Z

11 votes

如果您需要将寬度或高度設定為match_parent(與其父級一樣大)或ViewGroup.LayoutParams(大到足以适合其内部内容),則ViewGroup.LayoutParams具有以下兩個常量:

imageView.setLayoutParams(

new ViewGroup.LayoutParams(

// or ViewGroup.LayoutParams.WRAP_CONTENT

ViewGroup.LayoutParams.MATCH_PARENT,

// or ViewGroup.LayoutParams.WRAP_CONTENT,

ViewGroup.LayoutParams.MATCH_PARENT ) );

或者你可以像在Hakem Zaied的回答中那樣設定它們:

imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;

//...

Omar answered 2019-01-17T05:56:07Z

9 votes

您可以為所有案例設定值。

demoImage.getLayoutParams().height = 150;

demoImage.getLayoutParams().width = 150;

demoImage.setScaleType(ImageView.ScaleType.FIT_XY);

tienanhcntt2 answered 2019-01-17T05:56:31Z

8 votes

動态設定按鈕的最佳和最簡單的方法是

Button index=new Button(this);

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());

int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());

上述高度和寬度以像素為機關px。 45是dp的高度,42是dp的寬度。

index.setLayoutParams(new .LayoutParams(width, height));

是以,例如,如果您将按鈕放在TableLayout中的TableRow中,則應将其設定為TableRow.LayoutParams

index.setLayoutParams(new TableRow.LayoutParams(width, height));

Ali Kahoot answered 2019-01-17T05:57:17Z

7 votes

隻需建立一個LayoutParams對象并将其配置設定給imageView

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);

imageView.setLayoutParams(params);

Zubair Akber answered 2019-01-17T05:57:39Z

4 votes

image_view.getLayoutParams().height

以像素為機關傳回高度值。 您需要先擷取螢幕尺寸才能正确設定值。

Display display = context.getWindowManager().getDefaultDisplay();

DisplayMetrics outMetrics = new DisplayMetrics();

display.getMetrics(outMetrics);

int screen_height = outMetrics.heightPixels;

int screen_width = outMetrics.widthPixels;

獲得螢幕尺寸後,您可以使用适當的邊距設定圖像視圖寬度和高度。

view.getLayoutParams().height = screen_height - 140;

view.getLayoutParams().width = screen_width - 130 ;

Vikas answered 2019-01-17T05:58:09Z