调整图像大小以适合LinearLayout

时间:2014-09-04 00:19:05

标签: android android-layout

我需要根据用户选择的内容动态地将一些图像添加到LinearLayout,但我无法弄清楚如何在添加图像之前调整图像大小。我尝试了setWidth()setHeight()方法,但似乎什么也没做。我想将它们设置为某个dp。

感谢有人看到这个。

 ImageView image = new ImageView(getApplicationContext());

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.MATCH_PARENT,
               LinearLayout.LayoutParams.MATCH_PARENT);

image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER);

image.setImageResource(R.drawable.icon1);

LinearLayout layout = (LinearLayout) findViewById(R.id.iconLayout);
layout.addView(image, cart.size()-1, params);

2 个答案:

答案 0 :(得分:1)

<强>问题:

ImageView.ScaleType.CENTER

它正在做的是它会缩放你的图像,但它只适合x轴或y轴。

来自documentation

Compute a scale that will maintain the original src aspect ratio, 
but will also ensure that src fits entirely inside dst. At least 
one axis (X or Y) will fit exactly. The result is centered inside dst.

<强>溶液

您可以使用ImageView.ScaleType.FILL在x和y轴上缩放图片,具体取决于ImageView高度宽度。< / p>

image.setScaleType(ImageView.ScaleType.FILL);

答案 1 :(得分:1)

行。我想通过在正确的方向上操纵了它。我能够使用FIT_CENTER并完成它。

相关问题