我如何设置一个ImageView的宽度和高度编程?


当前回答

科特林

val density = Resources.getSystem().displayMetrics.density
view.layoutParams.height = 20 * density.toInt()

其他回答

以下是完成任务的简单方法:

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

希望对你使用充分。

的形象。setLayoutParams(新ViewGroup。LayoutParams(宽度、高度));

例子:

image.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
int newHeight = 150;
            int newWidth = 150; 
            holder.iv_arrow.requestLayout();
            holder.iv_arrow.getLayoutParams().height = newHeight;
            holder.iv_arrow.getLayoutParams().width = newWidth;
            holder.iv_arrow.setScaleType(ImageView.ScaleType.FIT_XY);
            holder.iv_arrow.setImageResource(R.drawable.video_menu);

简单地创建一个LayoutParams对象并将其分配给imageView

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

您可以为所有情况设置值。

demoImage.getLayoutParams().height = 150;

demoImage.getLayoutParams().width = 150;

demoImage.setScaleType(ImageView.ScaleType.FIT_XY);