ImageView adjustViewBounds似乎没有工作??(不同的项目不同的结果)

时间:2014-06-17 06:23:25

标签: android imageview

    我试图使用与父宽度匹配的ImageView,同时保持纵横比。结果与1个项目的结果一致。但在其他情况下,相同的代码给出了不同的结果。

仅供参考,布局来自两个不同的项目,而不是同一个项目。任何人都可以帮助我吗?

![项目1布局] [1]

    <?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="#000000" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/news_holder" />

</RelativeLayout>

![项目2布局] [2]

<?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="#000000" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/news_holder" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

你应该设置你的targetSDK 19,它给出了预期的结果。但是当设置为17时,问题就来了。从文档来看,这应该是原因。

来自the docs的注释:

  

如果应用程序的目标是API级别17或更低,请调整ViewBounds   将允许drawable缩小视图边界,但不会增长到   在所有情况下填充可用的测量空间。这是为了兼容性   使用传统的MeasureSpec和RelativeLayout行为。

答案 1 :(得分:0)

将您的代码更改为:

<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/news_holder" />

你应该设置一个高度让我们知道它有高度填充的imageview。在这种情况下150dp你可以设置你需要的任何东西。