围绕ImageView的不必要的填充 [英] Unwanted padding around an ImageView

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

问题描述

我需要包括在我所有的活动/视图的标题图片。与标题中的文件被称为header.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:后台=#0000FF
  机器人:填充=0dip>

  < ImageView的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:SRC =@可绘制/头
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =0dip
    机器人:layout_marginTop =0dip
    机器人:layout_marginBottom =0dip
    机器人:填充=0dip
    机器人:paddingTop =0dip
    机器人:paddingBottom会=0dip
    机器人:layout_gravity =补
    机器人:后台=#00FF00
    />
< /的FrameLayout>
 

注意机器人:背景=#00FF00(绿色),它只是可视化的目的

我包括他们进入我的看法是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
  机器人:方向=垂直
  风格=@风格/ white_background>

  <包括布局=@布局/头/>
  (...)
 

所以,当我真正尝试一下,结果看起来不是它应该是什么样子(右)如左图,:

(1)本 - 橙色 - 部分是图像/ ImageView的问题
(2)不被爱的绿色边框。注意:通常情况下,绿化面积将是透明的 - 这是唯一的绿色,因为我设置了背景

请注意图像的顶部周围的绿色边框;它的ImageView的的一部分,我只是想不通,为什么它的存在或如何我可以摆脱它。它设置所有填充和边距设置为0(但结果是一样的,当我忽略它​​们)。图像是JPEG 480x64px *和我把它放在RES /绘制(而不是在一绘制-Xdpi 虽然)。

(* JPEG,因为它似乎是我偶然发现了老巴伽玛问题 - 。起初我通过绿色边框相同的橙色作为画面工作围绕这一问题,而且颜色不匹配)

我想它在我的HTC Desire / 2.2 /编译2.33.163.1,并在模拟器上。此外,我所描述的问题有人在#Android的开发;她可以重现这个问题,但没有任何解释。建设目标为1.6。

更新@tehgoose:此code产生完全相同的顶部+底部填充的效果。

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
  机器人:方向=垂直
  风格=@风格/ white_background>

  <! - <包括布局=@布局/头/> - >

  < ImageView的
    机器人:SRC =@可绘制/头
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:后台=#00FF00
    机器人:layout_weight =0
    />

  <的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:填充=8DIP
    机器人:layout_weight =1>

    (...元素的其余部分)

  < / LinearLayout中>
< / LinearLayout中>
 

解决方案

终于来了!

 < ImageView的
  (...)
  机器人:adjustViewBounds =真/>
 

的<一个href="http://developer.android.com/reference/android/widget/ImageView.html#attr_android%3aadjustViewBounds">adjustViewbounds属性做的伎俩:

  

设置为真,如果你想在ImageView的调整其范围为preserve其绘制的纵横比。

我迷迷糊糊<一href="http://stackoverflow.com/questions/4236568/android-scrollview-adds-extra-padding-on-top-and-bottom-of-child-imageview/4255690#4255690">upon在这里的。感谢您的帮助!

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>

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) 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.

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, 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.)

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.

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>

解决方案

finally!

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

the adjustViewbounds attribute did the trick:

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天全站免登陆