简体   繁体   English

ImageView:adjustViewBounds不能与layout_height =“fill_parent”一起使用?

[英]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设置为false48dp与下面显示的图片类似:

            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.

相关问题 RelativeLayout中的layout_height =“fill_parent”不起作用 - layout_height=“fill_parent” in RelativeLayout doesn't work 在Android中,gravity =“ fill_vertical”与layout_height =“ fill_parent” - gravity=“fill_vertical” vs. layout_height=“fill_parent” in Android 将layout_height设置为fill_parent时,Android视图消失 - Android view disappears when setting layout_height to fill_parent imageView 属性 android:adjustViewBounds=true 是否与 layout_width=true 和 layout_height=true 具有完全相同的影响? - Does imageView attribute android:adjustViewBounds=true has exactly the same affect as: layout_width=true and layout_height=true? 具有高度wrap_content的ConstraintLayout不适用于ImageView AdjustViewBounds - ConstraintLayout with height wrap_content does not work with ImageView adjustViewBounds ViewPager宽度fill_parent不起作用 - ViewPager width fill_parent does not work RelativeLayout高度fill_parent在项目列表视图布局中不起作用 - RelativeLayout height fill_parent doesn't work in item listview layout 自定义对话框中的ImageView - fill_parent不起作用,我的对话框很小 - ImageView in a Custom Dialog - fill_parent does not work and my dialog is small imageView layout_height android - imageView layout_height android 为什么 android:layout_height 只能是填充父级或包装内容? - Why does android:layout_height can only be either fill parent or wrap content?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM