如何在运行时更改image_view的高度

时间:2016-02-03 10:28:00

标签: android

我试过了IMAGEVIEW.getLayoutParams().height = 200。但它没有正确改变。有没有其他可能的方法,请提出建议。

5 个答案:

答案 0 :(得分:2)

使用此

 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
    yourImageView.setLayoutParams(layoutParams);

答案 1 :(得分:1)

_image_view.getLayoutParams().height = 30;

希望这有帮助。

非常重要。

如果您在布局布局后再设置高度', 确保你也称之为:

_image_view.requestLayout()

答案 2 :(得分:1)

 ImageView mImageView = (ImageView)findViewById(R.id.image_view);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100); // Paramters are (width, height) in pixels
            mImageView.setLayoutParams(layoutParams);

如果您想要多种分辨率兼容性,

 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(getResources().getDimension(R.dimen.image_with)),
                (int) getResources().getDimension(R.dimen.image_height));
资源

下的

dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="image_with">100dp</dimen>
    <dimen name="image_height">100dp</dimen>
</resources> 

答案 3 :(得分:0)

如果您的imageview已经显示图片,

呼叫:

image_view.requestLayout(); 

答案 4 :(得分:0)

首先检查 Set ImageView width and height programmatically

您可以尝试 DisplayMetrics 逻辑。它更完美。

  

描述有关显示的一般信息的结构,例如   它的大小,密度和字体缩放。

   DisplayMetrics metrics = getResources().getDisplayMetrics();

   int DeviceTotalWidth = metrics.widthPixels;
   int DeviceTotalHeight = metrics.heightPixels;

   image_view.getLayoutParams().height= (int) (DeviceTotalHeight*5.25/100); // You can change from here

另一种方式

    LinearLayout.LayoutParams layoutParams = new  LinearLayout.LayoutParams(200, 200);
    IMAGEVIEW.setLayoutParams(layoutParams);
相关问题