首页 文章

ImageView周围不需要的填充

提问于
浏览
131

我需要在所有活动/视图中包含 Headers 图形 . 带 Headers 的文件名为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" />
  (...)

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

note the green border

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

注意顶部图像周围的绿色边框;它's part of the ImageView and I just can'弄清楚它为什么存在或如何摆脱它 . 它将所有填充和边距设置为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>

8 回答

  • 417

    最后!

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

    adjustViewbounds attribute做了诀窍:

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

    我偶然发现了upon it here . 谢谢你的帮助!

  • 0
    android:scaleType="fitXY"
    

    这个对我有用 .

  • 0

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

    • 更改图像的大小以适合指定的布局,或

    • 使用 android:scaleType 以适合图像 . 例如,您可以指定 android:scaleType="fitXY"

  • 0

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

    例如: android:layout_height="40dp"

  • 34
    android:layout_height="wrap_content"
    

    您需要将其更改为“fill_parent”

    您还可能需要缩放图像,以便填充其所在的framelayout是正确的比例 .

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

    EDIT: 是的,对不起,xml是imageview的高度 .

  • -1

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

  • 2

    那些额外的填充是自动生成的,因为android会尝试获得原始的宽高比 . 请尝试以下

    scaletype = "fitCenter"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"
    
  • 0

    您需要在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" />
    

相关问题