ImageView周围不需要的填充

ste*_*efs 140 user-interface android imageview

我需要在所有活动/视图中包含标题图形.带标题的文件名为header.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:background="#0000FF" 
  android:padding="0dip">

  <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dip"
    android:layout_marginTop="0dip"
    android:layout_marginBottom="0dip"
    android:padding="0dip"
    android:paddingTop="0dip"
    android:paddingBottom="0dip"
    android:layout_gravity="fill"
    android:background="#00FF00"
    />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

注意android:background="#00FF00"(绿色),它只是可视化的目的.

我将它们包含在我的视图中:

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

  <include layout="@layout/header" />
  (...)
Run Code Online (Sandbox Code Playgroud)

因此,当我实际尝试时,结果看起来像左图像,而不是它应该是什么样的(右):

注意绿色边框

(1)这 - 橙色 - 部分是图像/ ImageView
(2)不受欢迎的绿色边框.注意:通常,绿色区域是透明的 - 它只是绿色,因为我设置了background.

注意顶部图像周围的绿色边框; 它是ImageView的一部分,我无法弄清楚它为什么存在或我如何摆脱它.它将所有填充和边距设置为0(但是当我省略它们时结果是相同的).图像是480x64px jpeg*,我把它放在res/drawable中(不是在其中一个中drawable-Xdpi).

(*jpeg,因为我似乎偶然发现了旧的png伽玛问题 - 起初我通过使绿色边框与图片橙色相同来解决问题,并且颜色不匹配.)

我在我的htc desire/2.2/Build 2.33.163.1和模拟器上试了一下.我也向#android-dev中的某个人描述了这个问题.她可以重现这个问题,但也没有解释.构建目标是1.6.

更新@tehgoose:此代码产生完全相同的顶部+底部填充结果.

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

  <!-- <include layout="@layout/header" />  -->

  <ImageView
    android:src="@drawable/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00FF00"
    android:layout_weight="0"
    />

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dip"
    android:layout_weight="1">

    (... rest of the elements)

  </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ste*_*efs 430

最后!

<ImageView
  (...)
  android:adjustViewBounds="true" />
Run Code Online (Sandbox Code Playgroud)

adjustViewbounds属性的伎俩:

如果希望ImageView调整其边界以保持其drawable的纵横比,请将此属性设置为true.

在这里偶然发现了它.谢谢你的帮助!

  • 我有同样的问题,但这个解决方案没有效果. (5认同)
  • 哇...没有该属性,根据屏幕密度添加了不需要的填充,因此它只发生在某些设备上.它让我疯狂,因为我拥有的所有设备上的图像看起来很好,然后当我在朋友的设备上尝试它时,它看起来像垃圾.谢谢. (4认同)

ful*_*oon 39

android:scaleType="fitXY"
Run Code Online (Sandbox Code Playgroud)

这个对我有用.

  • `scaleType ="fitXY"`做了不同的事情:它改变了图像的纵横比,导致失真.这有时是可以接受的,例如对于抽象背景图像. (3认同)
  • 不完全是,但 android:scaleType="fitEnd" 就像魔术一样起作用。 (3认同)

MJ *_*tes 8

这些额外的填充是自动生成的,因为 android 会尝试获取原始纵横比。请尝试以下

   android:scaleType="fitCenter"
   android:adjustViewBounds="true"
   android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)