[英]Unwanted padding around an ImageView
我需要在我的所有活動/視圖中包含一個 header 圖形。 帶有 header 的文件稱為 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 伽馬問題 - 起初我通過使綠色邊框與圖片相同的橙色來解決這個問題,而 colors 不匹配。)
我在我的 htc 欲望/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>
最后!
<ImageView
(...)
android:adjustViewBounds="true" />
如果您希望 ImageView 調整其邊界以保持其可繪制對象的縱橫比,請將其設置為 true。
我在這里偶然發現了它。 感謝您的幫助!
android:scaleType="fitXY"
這個對我有用。
這些額外的填充是自動生成的,因為 android 會嘗試獲得原始縱橫比。 請在下面嘗試
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
它與圖像縱橫比有關。 默認情況下,系統保持圖像源的原始縱橫比。 在大多數情況下,這與布局中指定的布局或尺寸不完全匹配。 所以,
android:scaleType
來擬合圖像。 例如,可以指定android:scaleType="fitXY"
在 imageview xml 中使用此 android:scaleType="fitXY"
android:layout_height="wrap_content"
您需要將其更改為“fill_parent”
您可能還需要縮放圖像,使其成為填充其所在框架布局的正確比例。
我不明白你為什么需要那里的框架布局。 沒有它,您的圖像無論如何都會填滿屏幕。
編輯:是的,對不起,xml 是 imageview 的高度。
可能您可以給specific height to imageView say 50dp or 40dp
並adjust image to make green border
消失。
例如: android:layout_height="40dp"
您需要在 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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.