2011-12-27 127 views
31
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/messageLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/messageSender" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:id="@+id/messageSenderName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/messageSender" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:singleLine="false" 
     android:textColor="@color/list_text_color" 
     android:textSize="16dp" 
     android:layout_marginTop="5dp" /> 

    <TextView 
     android:id="@+id/messageContent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/messageSender" 
     android:layout_below="@id/messageSenderName" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:singleLine="false" 
     android:textColor="@color/codeFont" 
     android:textSize="13dp"/> 
</RelativeLayout> 

在我的佈局中,我有問題。當我設置marginTop="5dp"這很好,但是當我使用marginBottom時,我的佈局中沒有任何反應。另外,當我在RelativeLayout中設置填充時,它也不起作用。這裏有什麼問題?你能給我任何解決辦法嗎?如果設置爲android:layout_height="wrap_content"<RelativeLayout>,而不是將其設置爲match_parent和檢查爲什麼marginBottom不能正常工作?

回答

83

marginBottom沒有效果。

+0

'fill_parent'從API級別8開始被棄用,並由'match_parent'取代。 – 2013-06-09 03:31:05

+3

@RosePerrone是的我知道,但我認爲這個答案已發佈2年;) – 2013-06-09 05:29:45

+0

只需更新您的答案 – 2013-06-09 16:07:57

17

這真的只是<RelativeLayout>的一個bug,所以你可以嘗試將RelativeLayout包裝在<LinearLayout>的內部,並在那裏設置邊距。

+1

只需更改相對線性即可。 – Campino 2016-02-16 16:14:51

9

也許你可以設置android:paddingBottom=""<RelativeLayout>以獲得相同的效果。

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 – Naveen 2013-09-03 04:50:57

+0

@Naveen我不知道:) – 2013-09-03 05:08:53

+1

這是一個可接受的工作,否則難以理解的疑慮。 – Moritz 2014-08-20 10:23:03

13

我不確定在哪個版本的Android中遇到此問題。我在Android 4.2.2(https://android.googlesource.com/platform/frameworks/base/+/android-4.2.2_r1.1/core/java/android/widget/RelativeLayout.java)中查看了RelativeLayout,我相信onMeasure中存在一個錯誤。

在線路486到488,我們下面的代碼:

if (isWrapContentHeight) { 
    height = Math.max(height, params.mBottom); 
} 

在我看來,以上的應爲:

if (isWrapContentHeight) { 
    height = Math.max(height, params.mBottom + params.bottomMargin); 
} 

與Android:layout_height = 「WRAP_CONTENT」,RelativeLayout的不出現時會考慮最後垂直佈局視圖的底部末端。

我建議你嘗試以下之一:

*)添加一個虛擬視圖,這將是最後的垂直佈局視圖,並確保它具有了android:layout_below屬性。請注意,虛擬視圖沒有底部邊距,因此RelativeLayout會考慮其上方佈置的視圖的底部邊距。對於你的情況下,虛擬視圖應該是這樣的:

<View 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_below="@id/messageSender" 
/> 

*)正如其他人所說,實現在其他方面,如填充相同的效果。

*)一個不太平凡的解決方案是將RelativeLayout,其平臺樣式定義和Pool管理類引入到您的項目中,執行上面提到的錯誤修復,並在通常使用RelativeLayout的任何位置使用它。

*)文件與谷歌

希望這有助於一個錯誤。

+0

這已在'RelativeLayout'的最新版本中修復,但現在取決於您使用的目標SDK版本。在'Android 5.1.1'中,通過檢查如下版本來完成:'if(targetSdkVersion 2015-06-21 18:30:21

-3

它適用於您使用android:layout_marginBottom with android:layout_alignParentBottom="true"

例如:

android:layout_alignParentBottom="true" 
android:layout_marginBottom="50dp"> 
+0

不,它不,問題是layout_height =「wrap_content」 – Miquel 2016-11-04 15:42:30

1

確保你的根容器的layout_height設置爲match_parent而不是wrap_content