gpt4 book ai didi

android - ImageView adjustViewBounds 不适用于相对布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:44 25 4
gpt4 key购买 nike

我有一个 ImageView 并将其 layout_height 设置为“100dp”,并将其 layout_width 设置为“wrap_content”。

使用的 drawable 具有更大的实际尺寸,为 130dp(宽度)X 215dp(高度)。

ImageView 放置在 LinearLayout 中时,如果我将 adjustViewBounds 设置为 true,它会根据drawable 的宽高比和 layout_width。

但是,当放置在 RelativeLayout 中时,无论 adjustViewBounds 设置如何,ImageView 都会获得与 drawable 相同的宽度 (130dp)。

在这两种情况下,图像都正确呈现而没有失真,但是 ImageView 的边界不同。

相对布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">

<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:src="@drawable/phone_orange"
android:contentDescription="@null" />

</RelativeLayout>

ImageView in Relative Layout

线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:orientation="vertical" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:src="@drawable/phone_orange" />

</LinearLayout>

ImageView in LinearLayout

为什么 ImageView 在这两种情况下的测量方式不同?有没有办法让 ImageView 的行为总是像在 LinearLayout 的情况下一样(用 LinearLayout 包装它可行,但它不是一个干净的解决方案)?

最佳答案

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:adjustViewBounds="true"

I got it there

关于android - ImageView adjustViewBounds 不适用于相对布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035724/

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