【问题标题】:Fit a ImageView (and its src) to the layout width and make its height proportional使 ImageView(及其 src)适合布局宽度并使其高度成比例
【发布时间】:2011-08-12 14:05:45
【问题描述】:

你能帮我配置我的布局吗?我没有达到我想要的:

我已将 imageview 背景设为红色以说明我想要什么:

图像的宽度小于布局宽度。我希望图像按比例增长,保持其纵横比,直到达到第一张图像的纵横比(我用 photoshop 手动修改了它)但是当设置 width="fill_parent" 时结果是第二张图像并将宽度和高度都设置为“ fill_parent" 结果是第三张图片。

我尝试与 ScaleType 结合没有成功

如何实现第一个选项?谢谢

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    ImageView 的 xml 中使用 android:adjustViewBounds=true attr


    这里通过扩展 ImageView 类解决了这个问题。

    Android ImageView adjusting parent's height and fitting width


    我创建了一个示例。这个对我有用。这是xml布局文件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView android:src="@drawable/hydrangeas"
            android:layout_height="wrap_content" android:id="@+id/imageView1"
            android:scaleType="centerInside"
            android:adjustViewBounds="true"
            android:background="@android:color/white"
            android:layout_width="wrap_content"/>
    
    </LinearLayout>
    

    它也适用于fitCenter。看看快照。

    【讨论】:

    • 我真的不明白 adjustViewBounds 是如何工作的,但我没有达到我的目标你能解释一下怎么做吗?
    • 尝试同时使用 scaletype=centerInside。
    • 嗨,在我的情况下它也不起作用。较小的图像不会放大以适应父视图,但会留下一些边框。
    【解决方案2】:

    在你的 ImageView 标签中使用 fit_center 属性。

    <ImageView ... android:scaleType="fitCenter" .. />
    

    【讨论】:

    • 感谢您的快速答复。你的意思是 android:scaleType="fitCenter" 对吧?使用该选项和 fill_parent 的两个维度,结果类似于图像 3(图像在中间)。例如,使用 height="wrap_content" 结果是图像 2,可绘制在中心(水平)
    • 对两个参数都使用“wrap_content”,如果您使用的是 LinearLayout,请添加 android:layout_weight="1"
    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2016-09-20
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多