ImageView adjustViewBounds无法使用相对布局

Pet*_*eas 8 android android-layout android-imageview android-relativelayout

我有一个ImageView并设置layout_height为"100dp",其layout_width设置为"wrap_content".

使用的可绘制的实际尺寸更大,为130dp(宽度)×215dp(高度).

ImageView放置在a中时LinearLayout,如果我设置adjustViewBounds为true,它将获得根据drawable的纵横比和layout_width测量的宽度.

然而,放置在内部时RelativeLayoutImageView得到相同的宽度可提拉(130dp),不管adjustViewBounds设置.

在这两种情况下,图像都会呈现相关性而不会失真,但ImageView的界限是不同的.

RelativeLayout:

<?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>
Run Code Online (Sandbox Code Playgroud)

相对布局中的ImageView

LinearLayout:

<?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>
Run Code Online (Sandbox Code Playgroud)

LinearLayout中的ImageView

为什么ImageView这两种情况下的措施不同?有没有办法使ImageView行为始终像在LinearLayout情况下(用LinearLayout包装它工作,但它不是一个干净的解决方案)?

Kir*_*nov 14

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:adjustViewBounds="true"
Run Code Online (Sandbox Code Playgroud)

我明白了