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

标签 java android layout

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

我已将 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/

相关文章:

java - 对于 Marshmallow 之上的设备,在 Android 中以编程方式断开调用

android - 使用内容解析器和内容提供程序时应用程序崩溃

html - 创建如下布局的最佳方法?

java - 将 Logcat 保存到 Android 设备中的文本文件

java - 英雄库 : spring boot acces to endpoints with https only

java - 同时进行字段验证

java - 从谷歌数据流输出到谷歌云Firestore

android - 在Android中使用Volley进行错误处理

java - 如何使用多种布局,而面板之间没有空白?

安卓工作室 : Error parsing XML & URI is not registered