【问题标题】:Unwanted padding around an ImageViewImageView 周围不需要的填充
【发布时间】:2011-08-15 09:19:33
【问题描述】:

我需要在我的所有活动/视图中包含标题图形。带有标头的文件称为 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>

注意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" />
  (...)

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

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

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

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

我在我的 htc hope/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>

【问题讨论】:

  • 负边距和/或填充值也可以工作 -android:layout_margin="-10dp", android:padding="-10dp"

标签: android user-interface imageview


【解决方案1】:

终于!

<ImageView
  (...)
  android:adjustViewBounds="true" />

adjustViewbounds attribute 成功了:

如果您希望 ImageView 调整其边界以保持其可绘制对象的纵横比,请将其设置为 true。

我偶然发现了upon it here。感谢您的帮助!

【讨论】:

  • 我也有同样的问题,但是这个解决方案没有效果。
  • 哇...如果没有该属性,则会根据屏幕密度添加不需要的填充,因此仅在某些设备上发生。这让我发疯了,因为图像在我拥有的所有设备上看起来都很好,然后当我在朋友的设备上尝试时,它看起来就像垃圾。谢谢。
  • 感谢上帝,我将我的第一次尝试从尝试解决错误转向寻找安卓系统中的某些东西。我相信这个问题始于一些制造商从 android 更改东西,然后他们必须在下一个版本中添加一个属性来解决它。
  • 遗憾的是,这不适用于较低的 api,至少 api 16 和 17。
  • 哇,这正是我要找的东西,我挠了几个小时,甚至试图使我的布局无效并将其高度/宽度设置为图像的。如此简单的解决方案,无需 scaleType 即可完美运行。
【解决方案2】:
android:scaleType="fitXY"

它对我有用。

【讨论】:

  • scaleType="fitXY" 做了一些不同的事情:它改变了图像的纵横比,导致失真。有时这可能是可以接受的,例如用于抽象背景图像。
  • 我使用 android:width 和 android:height 控制了纵横比,而 scaleType="fitXY" 确实为我填充了所需的纵横比,这至少是我的经验。感谢您的意见。
  • 它可以工作,但图像不漂亮、不平衡、被裁剪
  • 不完全是,但 android:scaleType="fitEnd" 就像魔术一样工作。
  • 我发现 android:adjustViewBounds="true"android:scaleType="fitXY" 的组合是我在布局中对齐 ImageViews 所需要的。只有adjustViewBounds,图像偏离了一两个像素(在高分辨率显示器上非常小)。添加scaleType 似乎会在调整视图边界后强制对图像进行最终缩放,这会强制图像缩放到新边界的完整大小。
【解决方案3】:

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

   android:scaleType="fitCenter"
   android:adjustViewBounds="true"
   android:layout_height="wrap_content"

【讨论】:

    【解决方案4】:

    它与图像纵横比有关。默认情况下,系统保持图像源的原始纵横比。在大多数情况下,这与布局中指定的布局或尺寸不完全匹配。因此,

    1. 要么更改图像的大小以适应指定的布局,要么
    2. 使用android:scaleType 来适应图像。例如,您可以指定android:scaleType="fitXY"

    【讨论】:

      【解决方案5】:

      在 imageview xml 中使用这个 android:scaleType="fitXY"

      【讨论】:

        【解决方案6】:

        也许你可以给specific height to imageView say 50dp or 40dpadjust image to make green border消失。

        例如:android:layout_height="40dp"

        【讨论】:

        • 设置 layout_width="480px" 和 layout_height="64px" 产生我想要的结果,但像素不是很实用。老实说,我不明白这如何转化为 dp。
        • 您应该像使用像素一样使用 dp。他们就是这样;显示独立像素。但它有效。
        • dp/dip 表示与密度无关的像素;据我了解,它涵盖了不同的密度,但(当然)不是不同的屏幕尺寸。我想要的只是将图像缩放到 100% 屏幕宽度并保留高度,以便保持比例。
        【解决方案7】:
        android:layout_height="wrap_content"
        

        您需要将其更改为“fill_parent”

        您可能还需要缩放图像,使其成为填充其所在框架布局的正确比例。

        我完全不明白你为什么需要那里的框架布局。没有它,您的图像无论如何都会填满屏幕。

        编辑:是的,抱歉,xml 是 imageview 的高度。

        【讨论】:

        • 唯一要补充的是他需要设置ImageView的布局高度,这在你的回答中不是很明显:)
        • 我也试过没有周围的布局,没有改变任何东西。此外,当前图像只是一个占位符 - 很快就会有其他元素(证明周围布局的合理性)。我不希望图像与屏幕匹配,我希望它就在顶部。
        • 仔细检查您的图像顶部和底部是否有空白区域。如果没有,请尝试使用 android:scaleType="" 缩放 imageview 对象。应该有一款适合您的需求。
        【解决方案8】:

        您需要在 imageview 周围提供形状以提供更好的外观。

        这是我用过的:

        <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <!--<gradient android:startColor="#FFFFFF" android:endColor="#969696"
        
            android:angle="270">
        -->
        <gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF"
        
            android:angle="270">
        </gradient>
            <stroke android:width="2dp" android:color="@color/graycolor" />
        <corners android:radius="2dp" />
        <padding android:left="5dp" android:top="5dp" android:right="5dp"
            android:bottom="5dp" />
        

        【讨论】:

        • 哇!我已经使用过形状(用于标签背景),但老实说,我不明白我应该如何在这里应用它。
        • @Schnalle:制作两个shapes.xml文件和一个selector.xml。将图像背景应用为 selector.xml 文件。在选择器文件中为聚焦/选择和正常状态提供两种状态。
        猜你喜欢
        • 2017-01-24
        • 2021-01-04
        • 2014-08-09
        • 2017-02-22
        • 1970-01-01
        • 1970-01-01
        • 2014-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多