ImageView 周围不需要的填充 [英] Unwanted padding around an ImageView

查看:21
本文介绍了ImageView 周围不需要的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to include a header graphic in all of my activities/views. The file with the header is called 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"(绿色),它只是可视化目的.

Note the android:background="#00FF00" (green), it's just visualisation purposes.

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

I include them into my views like this:

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

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

So, when I actually try it out, the result looks like the left image, instead of what it should look like (right):

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

(1) This - the orange - part is the image/ImageView in question
(2) The unloved green border. note: normally, the green area would be transparent - It's only green because I set the background.

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

Note the green border around the image at the top; It's part of the ImageView and I just can't figure out why it is there or how I can get rid of it. It set all paddings and margins to 0 (but the result is the same when I omit them). The image is a 480x64px jpeg* and I put it in res/drawable (not in one of the drawable-Xdpi though).

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

(* jpeg, because it seems I stumbled upon the old png gamma problem - at first I worked around the problem by making the green border the same orange as the picture, and the colors didn't match.)

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

I tried it on my htc desire/2.2/Build 2.33.163.1 and on the emulator. Also I described the problem to someone in #android-dev; She could reproduce the problem but had no explanation either. build target is 1.6.

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

update @tehgoose: this code yields the exact same top+bottom padded result.

<?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>

推荐答案

终于!

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

adjustViewbounds 属性做到了:

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

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

我偶然发现 在这里.感谢您的帮助!

i stumbled upon it here. thanks for your help!

这篇关于ImageView 周围不需要的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆