安卓XML布局中,使用layout_height="wrap_content"的ImageView具有顶部和底部填充。

54

我有一个垂直的LinearLayout,其中包含一个ImageView和几个其他布局和视图。

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/banner_alt"
        android:src="@drawable/banner_portrait" />

    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/main_search"
      android:gravity="center"
      android:textStyle="bold" />

    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

      <Spinner
        android:id="@+id/search_city_spinner"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:prompt="@string/search_city_prompt"
        android:entries="@array/search_city_array" />

      <Spinner
        android:id="@+id/search_area_spinner"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:prompt="@string/search_area_prompt"
        android:entries="@array/search_area_array" />

    </LinearLayout>

    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

      <Spinner
        android:id="@+id/search_rooms_spinner"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:prompt="@string/search_rooms_prompt"
        android:entries="@array/search_rooms_array" />

      <Spinner
        android:id="@+id/search_min_spinner"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:prompt="@string/search_min_prompt"
        android:entries="@array/search_min_array" />

      <Spinner
        android:id="@+id/search_max_spinner"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:prompt="@string/search_max_prompt"
        android:entries="@array/search_max_array" />

    </LinearLayout>

    <Button
      android:id="@+id/saearch_button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/search_button"
      android:onClick="searchButton" />

</LinearLayout>

我的问题是当Activity显示时,ImageView在顶部和底部有一些padding。我通过在ImageView上设置背景颜色确认了这一点。

图像的尺寸为450x450像素。手动将高度设置为450像素可以产生期望的效果(没有padding),而将其设置为450dp会产生与使用wrap_content相同的效果。

似乎Android正在将图像的高度(450px)作为值,在dp中设置ImageView的高度。

你有什么想法可以解决这个问题吗?我不想使用绝对值,因为我将为不同的屏幕密度提供不同的图像。

1个回答

138

我遇到了类似的问题,并使用 android:adjustViewBounds="true" 在 ImageView 上解决了它。

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:contentDescription="@string/banner_alt"
    android:src="@drawable/banner_portrait" />

15
这对我很有帮助。这似乎与直觉相反,因为这不是默认设置。它是“wrap_content”-但实际上并不是... - erlando
2
对我没用。最终我添加了一个监听器,在图像布局发生更改时手动调整图像大小以匹配正确的长宽比。 - gsteinert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接