[英]ImageView: adjustViewBounds does not work with layout_height=“fill_parent”?
I'm trying to place in single row the EditText
with the ImageView
on the left. 我试图将EditText
与左侧的ImageView
放在一起。 But I can't get the image to be scaled properly to match the height of text entry. 但我无法正确缩放图像以匹配文本输入的高度。
The layout is simple: 布局很简单:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:background="#f00"
android:src="@drawable/icon" />
<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
(I highlighted image background by red color to see the actual space allocated by ImageView) (我用红色突出显示图像背景,以查看ImageView分配的实际空间)
If I specify the exact height for ImageView
: 如果我指定ImageView
的确切高度:
android:layout_height="48dp"
then I get the closest view what I needed: 然后我得到了我最需要的观点:
But I do not know the exact height of EditText
, so I can't specify it for ImageView
here. 但是我不知道EditText
的确切高度,所以我不能在这里为ImageView
指定它。
When the height for ImageView
is specified to fill its parent (to match ``EditText` height): 指定ImageView
的高度以填充其父级(以匹配``EditText`高度):
android:layout_height="fill_parent"
then I get unexpected extra margin between the image and text entry: 然后我在图像和文本输入之间得到意想不到的额外余量:
Actually, in this case the ImageView
width equals to unscaled image width, whereas the image was scaled. 实际上,在这种情况下, ImageView
宽度等于未缩放的图像宽度,而图像是缩放的。
It's the similar to picture shown below if I specify layout_height
to 48dp
and set adjustViewBounds
to false
: 如果我将layout_height
指定为48dp
并将adjustViewBounds
设置为false
, 48dp
与下面显示的图片类似:
android:layout_height="48dp"
android:adjustViewBounds="false"
So, the question here is: how to define the layout correctly to scale the image to match edit entry height and in the same time to have width of ImageView
to be shrunk to scaled image width ? 因此,这里的问题是:如何正确定义布局以缩放图像以匹配编辑条目高度,同时将ImageView
宽度缩小到缩放图像宽度? In other words how to get rid this extra space off? 换句话说,如何摆脱这个额外的空间?
This is a LinearLayout bug. 这是一个LinearLayout错误。 If you look at forceUniformHeight
in the source here . 如果你在源代码中查看 forceUniformHeight
。 You can see that to handle sizing child views which supply match_parent
as the height when the LinearLayout is set to wrap_content
, the LinearLayout pretends to have an absolute height, recalculates the child height and then reuses the child width from the previous measure pass. 你可以看到,为了处理当LinearLayout设置为wrap_content
时将match_parent
作为高度提供的大小子视图,LinearLayout假装具有绝对高度,重新计算子高度,然后重用上一个度量过程中的子宽度。 Basically your image is calculating height and width unconstrained and getting default width and height, and then having height recalculated but being forced to use the old width. 基本上你的图像计算高度和宽度不受约束,并获得默认的宽度和高度,然后重新计算高度,但被迫使用旧的宽度。 You will need to supply a height or override some View onMeasures
您需要提供一个高度或覆盖一些View onMeasures
you can also put gravity to be centered on the linear layout 您还可以将重力放在线性布局的中心位置
NOTE: that you should also have different images in drawables-hdpi/ldpi and mdpi 注意:您还应该在drawables-hdpi / ldpi和mdpi中使用不同的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.