【发布时间】: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