The height of an image I will set by setImageBitmap() is more that 40dp. Using this layout I have an extra space between ImageView and TextView, where did it come from?
Since the android:layout_height="40dp"
with a android:scaleType="fitStart"
, the image is getting scaled down (and to the left/start) to fit the height, so this "extra space" is actually the original width of the image, since your android:layout_width="wrap_content"
. I recreated your layout with my own image that is larger than 40dp
. When you select the ImageView
, you can see that its bounds stretch to the image's original width.
To further prove this, if I set android:scaleType="center"
, no fit is applied and you can see the ImageView
's original width.
Can you guys explain why shall I put ImageView into FrameLayout to have things as intended?
It appears that since your FrameLayout
uses android:layout_width="wrap_content"
, it gets the correct scaled down width from your ImageView
after it gets scaled due to android:adjustViewBounds="true"
. It is strange that the ImageView
itself uses android:layout_width="wrap_content"
, but shows the original image's width, and not the scaled width. My hunch is that the height and width of ImageView
s get set before the scaling is applied, so the ImageView
gets the original image's width, but the parent FrameLayout
gets the scaled width of it's ImageView
child after the scaling is applied. This may not be true, but it appears that way to me.
However, you can solve the unscaled width issue (without using a FrameLayout
) by using android:maxHeight="40dp"
on the ImageView
. You can then set android:layout_height="wrap_content"
so your images can be smaller than 40dp
if the image is smaller.
<ImageView
android:id="@+id/companyIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:scaleType="fitStart" />
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…