【问题标题】:How to change the ImageView Height an Width [duplicate]如何更改 ImageView 高度和宽度 [重复]
【发布时间】:2017-05-23 08:34:40
【问题描述】:

我通过 Java 代码创建了一个新的ImageView,但是当我尝试使用setMaxHeight 设置高度时,它不起作用。有人可以帮我改变那个 ImageView 的高度吗?

ImageView i = new ImageView(MainActivity.this);
i.setImageResource(R.drawable.red);
i.setMaxWidth(50);
i.setMaxHeight(50);
ll.addView(i);

【问题讨论】:

标签: android imageview


【解决方案1】:

试试这个:

 i.getLayoutParams().height = 20;

【讨论】:

【解决方案2】:

简单的方法是像这样以编程方式设置大小:

i.setLayoutParams(new LayoutParams(width, height));

如果您想将 dp 更改为像素,您可以使用此代码:

public static float pxFromDp(final Context context, final float dp) {
    return dp * context.getResources().getDisplayMetrics().density;
}

i.setLayoutParams(new LayoutParams(pxFromDp(this, 50), pxFromDp(this, 50)));

【讨论】:

    【解决方案3】:

    使用此代码设置宽度和高度。

    ImageView iv = new ImageView(context);
    int width = 60;
    int height = 60;
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
    iv.setLayoutParams(parms);
    

    【讨论】:

      【解决方案4】:
      your_image_view.getLayoutParams().width = 100;**
      your_image_view.getLayoutParams().height = 100;**
      

      你找到了答案Here

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2014-05-01
        • 1970-01-01
        • 2023-03-18
        • 2018-10-28
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 2017-11-26
        • 1970-01-01
        相关资源
        最近更新 更多