为什么我需要换一个的ImageView成的FrameLayout? [英] Why do I need to wrap an ImageView into a FrameLayout?
问题描述
下面是一个简单的布局:
< RelativeLayout的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< ImageView的
机器人:ID =@ + ID / companyIcon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =40dp< - 通知我有限的高度 - >
机器人:scaleType =fitStart
机器人:adjustViewBounds =真
机器人:layout_alignParentLeft =真/>
<的TextView
机器人:ID =@ + ID /的companyName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ ID / companyIcon
机器人:layout_marginLeft =3DP
机器人:layout_centerVertical =真
机器人:TEXTSTYLE =黑体
机器人:文字颜色=#20526d/>
< / RelativeLayout的>
我会 setImageBitmap设置图像的高度()
更是40dp。
使用这个布局我有的ImageView之间多余的空格
和的TextView
,它是在哪里来的呢?
但是在我包裹的ImageView
与的FrameLayout
我没有这种不必要的额外的空间:
< RelativeLayout的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
<的FrameLayout
机器人:ID =@ + ID / image_container
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< ImageView的
机器人:ID =@ + ID / companyIcon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =40dp
机器人:scaleType =fitStart
机器人:adjustViewBounds =真
机器人:layout_alignParentLeft =真/>
< /的FrameLayout>
<的TextView
机器人:ID =@ + ID /的companyName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ ID / image_container
机器人:layout_marginLeft =3DP
机器人:layout_centerVertical =真
机器人:TEXTSTYLE =黑体
机器人:文字颜色=#20526d/>
< / RelativeLayout的>
和结果:
你们能解释为什么我该怎么说的ImageView
到的FrameLayout
来有事情如预期?非常感谢你。
我将由setImageBitmap设定(图像的高度)是更多的40dp。使用这个布局我的ImageView和TextView中之间的额外的空间,它是在哪里来的呢?
由于安卓layout_height =40dp
与安卓scaleType =fitStart
,图像是越来越缩小(和左/启动),以适应高度,所以这个额外空间实际上是图像的原始宽度,因为你的机器人:layout_width =WRAP_CONTENT
。我重新布局与我自己的形象,不能大于 40dp
。当您选择的ImageView
,你可以看到它的边界延伸到图像的原始宽度。
为了进一步证明这一点,如果我设置安卓scaleType =中心
,不适合被应用,你可以看到 ImageView的
的原始宽度。
你们可以解释为什么我该怎么说的ImageView到的FrameLayout有东西如预期?
看来,因为你的的FrameLayout
使用机器人:layout_width =WRAP_CONTENT
,它得到正确的按比例缩小从你的 ImageView的宽度
之后,它被因缩放安卓adjustViewBounds =真正的
。令人奇怪的是,的ImageView
本身使用机器人:layout_width =WRAP_CONTENT
,但显示了原始图像的宽度,不是缩放宽度。我的预感是的ImageView的高度和宽度
取值被置之前的应用缩放,因此的ImageView
得到原始图像的宽度,但父的FrameLayout
得到它的缩放宽度的 ImageView的
孩子在的应用缩放。这可能不是真的,但现在看来,这样对我。
不过,您可以使用的Android解决未缩放宽度的问题(不要使用
的FrameLayout
) $ C>在的ImageView
。然后,您可以设置安卓layout_height =WRAP_CONTENT
让您的图片可以比 40dp
小如果图像较小。
< ImageView的
机器人:ID =@ + ID / companyIcon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:adjustViewBounds =真
机器人:=了maxHeight40dp
机器人:scaleType =fitStart/>
Here is a simple layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/companyIcon"
android:layout_width="wrap_content"
android:layout_height="40dp" <!-- notice I've limited a height -->
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/companyIcon"
android:layout_marginLeft="3dp"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textColor="#20526d" />
</RelativeLayout>
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?
But after I wrap the ImageView
with FrameLayout
I don't have this unnecessary extra space:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/companyIcon"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true" />
</FrameLayout>
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image_container"
android:layout_marginLeft="3dp"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textColor="#20526d" />
</RelativeLayout>
And the result:
Can you guys explain why shall I put ImageView
into FrameLayout
to have things as intended? Thank you very much.
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" />
这篇关于为什么我需要换一个的ImageView成的FrameLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!