【问题标题】:ImageView in android XML layout with layout_height="wrap_content" has padding top & bottom具有 layout_height="wrap_content" 的 android XML 布局中的 ImageView 具有顶部和底部填充
【发布时间】:2012-03-22 04:09:24
【问题描述】:

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

<?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 像素。手动将高度设置为 450px 会产生所需的效果(无填充),将其设置为 450dp 会产生与使用 wrap_content 相同的效果。

似乎android正在获取图像的高度(450px)并将ImageView的高度设置为相同的值,但是在dp中。

关于我能做些什么来解决这个问题有什么想法吗?我不想使用绝对值,因为我将为不同的屏幕密度提供不同的图像。

【问题讨论】:

    标签: android xml layout imageview


    【解决方案1】:

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

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

    【讨论】:

    • 这对我有用。这不是默认设置似乎违反直觉。它是“wrap_content”——但不是真的……
    • 对我没用。我最终添加了一个侦听器,以在图像布局发生变化时手动调整图像大小以匹配正确的纵横比。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多