2012-04-30 37 views
9

我有以下佈局文件:爲什麼在使用wrap_content時忽略layout_marginBottom?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#FF0000" > 

<RelativeLayout 
    android:id="@+id/usericon_img" 
    android:layout_width="73.3334dp" 
    android:layout_height="70.6667dp" 
    android:layout_marginBottom="2.6667dp" 
    android:layout_marginLeft="3.3334dp" 
    android:layout_marginTop="2.6667dp" 
    android:background="#FFFFFF" /> 
</RelativeLayout> 

在模擬器(我測試過它在真實設備上,它看起來相同)的佈局是這樣的:enter image description here

我的問題是:爲什麼我在白色佈局下沒有紅色邊距?我知道,如果我改變外佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="76dp" 
android:background="#FF0000" > 

我會得到我想要的東西,但爲什麼不WRAP_CONTENT正常工作?

回答

6

documentation

爲了測量其尺寸的圖考慮到其填充。

如果我沒有弄錯,這意味着保證金不會添加到維度。這意味着wrap_content正常工作,但不考慮保證金。

只是一個簡短的問題:爲什麼使用像70.6667dp這樣的值?

+0

我已經使用了70.6667,因爲我想在hdpi屏幕上顯示106像素的尺寸。 – Toorop

+0

如果邊距被忽略,爲什麼我有左邊距和上邊距? – Toorop

+1

嘗試使用填充代替 – Ahmad