gpt4 book ai didi

java - 使 ImageView(及其 src)适合布局宽度并使其高度成比例

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:22 25 4
gpt4 key购买 nike

你能帮我配置我的布局吗?我没有实现我正在寻找的东西:

我已将 imageview 背景设置为红色以解释我想要的内容:

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

我尝试结合 ScaleType 但没有成功

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

enter image description here

最佳答案

ImageView 的 xml 中使用 android:adjustViewBounds=true 属性


这里通过扩展 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。看看快照。
With centerInside and adjustViewBounds="true"

关于java - 使 ImageView(及其 src)适合布局宽度并使其高度成比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7041257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com