ImageView adjustViewBounds无法使用相对布局 [英] ImageView adjustViewBounds not working with Relative Layout

查看:213
本文介绍了ImageView adjustViewBounds无法使用相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ImageView ,并将 layout_height 设置为100dp,将layout_width设置为wrap_content。

I have an ImageView and have set its layout_height to "100dp" and its layout_width to "wrap_content".

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

The drawable used has bigger actual dimensions that are 130dp (width) X 215dp (height).

ImageView 放在 LinearLayout 中,如果我设置 adjustViewBounds 为true,它的宽度是根据drawable的宽高比和layout_width测量的。

When the ImageView is placed inside a LinearLayout, if I set adjustViewBounds to true, it gets a width that is measured according to the drawable's aspect ratio and the layout_width.

然而,当放在 RelativeLayout <时/ code> ImageView 获得与drawable(130dp)相同的宽度,无论adjustViewBounds设置如何。

However, when placed inside a RelativeLayout the ImageView gets the same width as the drawable (130dp), no matter the adjustViewBounds settings.

在这两种情况下,图像渲染时都没有失真,但ImageView的界限是不同的。

In both cases the image renders correclty without distortion, however the ImageView's bounds are different.

RelativeLayout:

The 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>

LinearLayout:

The 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>

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

Why does the ImageView measures differently in these 2 scenarios? Is there a way to make the ImageView behave always like being in the LinearLayout case (wrapping it with a LinearLayout works but it's not a clean solution)?

推荐答案

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

我收到了它

这篇关于ImageView adjustViewBounds无法使用相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆