在android中更改imageview的高度和宽度

在android中更改imageview的高度和宽度,android,layout,height,imageview,width,Android,Layout,Height,Imageview,Width,在我的应用程序中,单击按钮时,我想更改imageview的大小。单击一个按钮时,我想将imageview的大小增加5(每个宽度和高度)。单击另一个按钮时,我想减小大小。我尝试了这个方法。 我该怎么做?下面是代码。它不工作 public void increase(View v) { int height=imageView.getHeight(); int width=imageView.getWidth(); imageView.getLayoutParams().

在我的应用程序中,单击按钮时,我想更改imageview的大小。单击一个按钮时,我想将imageview的大小增加5(每个宽度和高度)。单击另一个按钮时,我想减小大小。我尝试了这个方法。 我该怎么做?下面是代码。它不工作

 public void increase(View v)
{

    int height=imageView.getHeight();
    int width=imageView.getWidth();
    imageView.getLayoutParams().height = height+5;
    int h=imageView.getHeight();
    System.out.println("hhhhhhhhhh,,,,,,,,,,"+h);
    System.out.println("width n hight..."+width+"and"+height);


}

更新:

这样做:

int height=imageView.getHeight();
int width=imageView.getWidth();
imageView.setLayoutParams(new LinearLayout.LayoutParams(height, width));

建议您的ImageView位于线性布局内,您需要执行以下操作:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
params.setHeight(XX);
params.setWidth(XX);
imageView.setLayoutParams(params);

我不知道语法是否完全正确,但基本上就是这样做的,不要忘记需要更新UI才能看到任何更改。

我使用以下代码:

your_image_view.getLayoutParams().height = 20;
希望它能帮助你


您可以尝试以下替代方法。。看看它是否有效:

LayoutParams params = (LayoutParams) imageView.getLayoutParams();
params.width = 120;
params.height = 120;

imageView.setLayoutParams(params);

虽然这两个代码几乎相同…

每次单击按钮时,我想增加5,但您只需设置新的高度/宽度:params.setHeight(height+5)和您可以通过已定义的调用获得的高度:int height=imageView.getHeight();类型paramsey没有方法setHeight n width实际上我在尝试第二个代码。但是现在我尝试了第一个caode。它真的很有效。非常感谢。params没有像setWidth和setHeight这样的方法。请更新您的答案。它不起作用。我尝试了-'int h=imageView.getHeight();int w=imageView.getWidth();imageView.getLayoutParams().height=h+5;imageView.getLayoutParams().width=w+5;'有什么例外吗?您可以尝试以下替代方法。。看看它是否有效