我如何以编程方式设置
ImageView
的宽度和高度?
以编程方式设置ImageView的宽度和高度?
这可能为时已晚,但为了其他有同样问题的人,要设置
ImageView
的高度。
imageView.getLayoutParams().height = 20;
重要的是。如果你是在布局已经 "布置好 "之后再设置高度,请确保你也调用。
imageView.requestLayout();
如果你是在布局已经 "布置好 "之后才设置高度,请确保你调用image_view.requestLayout()
如果我需要wrap_content怎么办?
@FranciscoCorralesMorales 将此用于wrap_content
image_view.getLayoutParams().height = RelativeLayout.LayoutParams.WRAP_CONTENT
......如果视图的父代是
LinearLayout
则用它替换
RelativeLayout
。
So, how to specify the
unit
?如何说它是
dp
, not
sp
, etc.
CoolMind
:
@SaeedNeamati,这是
px
.为了将dp转换为px,可以使用一些方法,例如,在
stackoverflow.com/questions/4605527/converting-pixels-to-p
.
InvulgoSoft
发布于
2020-05-07
0
人赞同
如果你的图像视图是动态的,包含getLayout的答案将以null-exception失败。
在这种情况下,正确的方法是。
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
如果我放了这段代码,我的图像就消失了。为什么? ImageView imgLogo = new ImageView(this); imgLogo.setImageDrawable(logo); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(32,32); imgLogo.setLayoutParams(layoutParams); row.addView(imgLogo)。
谢谢你的回答,让我走上了正轨。作为补充,LinearLayout应该被设置为图像的父视图类型。例如,如果你的图像视图是在一个画廊中,你应该使用画廊的布局参数而不是LinearLayout,否则你会出现铸造异常。
@MustafaGüven 我和你有同样的问题,我的
ImageView
消失了。诀窍是使布局部分(即
<this_part_here>.LayoutParams
)与
ImageView
的父部分相同。因此,例如,如果你把你的
ImageView
放在一个
TableRow
的
TableLayout
中,你应该把它作为
TableRow.LayoutParams
。
MadBoomy
:
to elaborate on user($some_number) -> you need to
创建
的LayoutParams,根据父级和母级
set
他们在孩子(imageView)上,还有requestLayout()在写作时似乎没有必要。
user924
:
lol 你创建了新的参数,不好回答,你只是可以得到当前的参数,我的意思是如果你这样做(创建新的参数),那么你甚至不需要xml,然后也可以通过程序创建/添加imageview...
duggu
发布于
2020-05-07
0
人赞同
这种简单的方式来完成你的任务。
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);
hope use full to you.
这对我来说是很好的工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 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)。
n00dles
:
为什么人们要声明
int
的变量,如果它们不会在其他地方被重复使用?似乎没有必要,但我对这个问题了解不多。
duggu
:
只是为了更好地理解 @n00dles ,,,没有别的意思
Hiren Patel
发布于
2020-05-07
0
人赞同
我曾用
pixel
和
dp
玩过这个。
private int dimensionInPixel = 200;
How to set by pixel:
view.getLayoutParams().height = dimensionInPixel;
view.getLayoutParams().width = dimensionInPixel;
view.requestLayout();
How to set by dp:
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
view.getLayoutParams().height = dimensionInDp;
view.getLayoutParams().width = dimensionInDp;
view.requestLayout();
Hope this would help you.
Omar
发布于
2020-05-07
0
人赞同
如果你需要将你的宽度或高度设置为
match_parent
(与它的父级一样大)或
wrap_content
(大到足以容纳它自己的内部内容),那么
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;
//...
tienanhcntt2
发布于
2020-05-07
0
人赞同
你可以为所有情况设置数值。
demoImage.getLayoutParams().height = 150;
demoImage.getLayoutParams().width = 150;
demoImage.setScaleType(ImageView.ScaleType.FIT_XY);
原因是: java.lang.NullPointerException。试图在一个空对象引用上向字段'int android.view.ViewGroup$LayoutParams.height'写东西
Ali Kahoot
发布于
2020-05-07
0
人赞同
动态设置一个按钮的最好和最简单的方法是
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 being the height in dp和42为宽度,单位为dp.
index.setLayoutParams(new <Parent>.LayoutParams(width, height));
因此,例如,如果你把你的按钮放在TableLayout中的TableRow里,你应该把它作为TableRow.LayoutParams
index.setLayoutParams(new TableRow.LayoutParams(width, height));
Zubair Akber
发布于
2020-05-07
0
人赞同
简单地创建一个LayoutParams对象并将其分配给你的imageView
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
imageView.setLayoutParams(params);
Vikas
发布于
2020-05-07
0
人赞同
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;
After you get the screen dimensions, you can set the imageview width&height using proper margins.
view.getLayoutParams().height = screen_height - 140;
view.getLayoutParams().width = screen_width - 130 ;
Alireza pahlavani
发布于
2020-05-07
0
人赞同
image.setLayoutParams(new ViewGroup.LayoutParams(width, height))。
image.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
BharathRao
发布于
2020-05-07
0
人赞同
为了以编程方式设置ImageView和高度,你可以这样做
//Makesure you calculate the density pixel and multiply it with the size of width/height
float dpCalculation = getResources().getDisplayMetrics().density;
your_imageview.getLayoutParams().width = (int) (150 * dpCalculation);
//Set ScaleType according to your choice...
your_imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
William Hu
发布于
2020-05-07
0
人赞同
kotlin
val density = Resources.getSystem().displayMetrics.density
view.layoutParams.height = 20 * density.toInt()
Rohit Mandiwal
发布于
2020-05-07
0
人赞同
在需要以编程方式设置小组件尺寸的情况下,确保以下规则。
Set
LayoutParam
for the Layout in which you are adding that view
. In my case I am adding to
TableRow
so I had to do
TableRow.LayoutParams
Follow this code
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (17 * scale);
int dpHeightInPx = (int) (17 * scale);
TableRow.LayoutParams deletelayoutParams = new TableRow.LayoutParams(dpWidthInPx, dpHeightInPx);
button.setLayoutParams(deletelayoutParams);
tableRow.addView(button, 1);
raj kavadia
发布于
2020-05-07
0
人赞同
如果你想在图片视图中仅仅适合图片,你可以在高度和宽度属性中使用 "wrap content "和scale-type,但如果你想手动设置,你必须使用LayoutParams。
Layoutparams对于以编程方式设置布局的高度和宽度很有效。
MEGHA DOBARIYA
发布于
2020-05-07
0
人赞同
int newHeight = 150;
int newWidth = 150;
holder.iv_arrow.requestLayout();