Подтвердить что ты не робот

ImageView в XML-макете android с layout_height = "wrap_content" имеет отступы сверху и снизу

У меня есть вертикальный 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>

Моя проблема заключается в том, что при отображении активности ImageView имеет прописку сверху и снизу. Я подтвердил, что это ImageView (путем установки цвета фона в ImageView).

Изображение 450x450 пикселей. Установка высоты вручную на 450 пикселей создает желаемый эффект (без отступов), а установка его на 450 дп производит тот же эффект, что и при использовании wrap_content.

Кажется, что андроид занимает высоту изображения (450 пикселей) и устанавливает высоту ImageView на одно и то же значение, но в dp.

Любые идеи относительно того, что я могу сделать, чтобы исправить это? Я не хочу использовать абсолютные значения, поскольку я буду предоставлять разные изображения для разных плотностей экрана.

4b9b3361

Ответ 1

У меня была симулятивная проблема и разрешил ее с помощью 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" />