【问题标题】:changing height and width of imageview in android在android中改变imageview的高度和宽度
【发布时间】:2023-03-18 08:23:01
【问题描述】:

在我的应用程序中单击按钮时,我想更改 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);


}

【问题讨论】:

    标签: android layout height imageview width


    【解决方案1】:

    我使用以下代码:

    your_image_view.getLayoutParams().height = 20;
    

    希望对你有帮助..

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

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

    虽然两个代码几乎相同...

    【讨论】:

    • 它不工作。我试过了 - ' int h=imageView.getHeight(); int w=imageView.getWidth(); imageView.getLayoutParams().height = h+5; imageView.getLayoutParams().width=w+5; '
    • 会不会出现异常?
    • 您可以尝试以下替代方法.. 看看它是否有效
    【解决方案2】:

    更新:

    这样做:

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


    建议您的 ImageView 位于 LinearLayout 内,您需要这样做:

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

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

    【讨论】:

    • 每次点击按钮我想增加5
    • 你只需要设置新的高度/宽度:params.setHeight(height + 5) 和你已经定义的调用可以获得的高度:int height=imageView.getHeight();跨度>
    • 类型参数没有方法 setHeight n width
    • 嘿,实际上我正在尝试第二个代码。但现在我尝试了第一个 caode。它确实有效。非常感谢。
    • params 没有 setWidth 和 setHeight 这样的方法。请更新您的答案。
    猜你喜欢
    • 2011-05-15
    • 2012-02-11
    • 2010-11-02
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多