2015-06-12 93 views
1

我正在學習一個教程,我不明白爲什麼我應用於包含在相對佈局中的列表視圖的邊距不影響我在設備上測試時的外觀。但是,如果我將所有內容放在LinearLayout容器中,看起來應該如此。如果沒有這個LinearLayout,我不明白爲什麼佈局不起作用。爲列表視圖添加頁邊距

這是我的代碼在佈局我的ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
      android:background="@android:color/transparent"> 
<RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/inbox_horizontal_margin" 
    android:layout_marginRight="@dimen/inbox_horizontal_margin" 
    android:layout_marginTop="@dimen/inbox_vertical_margin" 
    android:background="@android:color/white"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/messageIcon" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/message_list_item_vertical_margin" 
     android:paddingBottom="@dimen/message_list_item_vertical_margin" 
     android:layout_alignParentStart="true" 
     android:contentDescription="@string/content_desc_message_icon" 
     android:src="@drawable/ic_action_picture"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/senderLabel" 
     android:layout_centerVertical="true" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:layout_toRightOf="@+id/messageIcon" 
     android:layout_toEndOf="@+id/messageIcon"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text=" created at: 03.17.15" 
     android:id="@+id/createdAtLabel" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/senderLabel" 
     android:layout_toEndOf="@+id/senderLabel"/> 
</RelativeLayout> 
</LinearLayout> 

這是代碼的結果: enter image description here

這是沒有的LinearLayout代碼:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/inbox_horizontal_margin" 
    android:layout_marginRight="@dimen/inbox_horizontal_margin" 
    android:layout_marginTop="@dimen/inbox_vertical_margin" 
    android:background="@android:color/white"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/messageIcon" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/message_list_item_vertical_margin" 
     android:paddingBottom="@dimen/message_list_item_vertical_margin" 
     android:layout_alignParentStart="true" 
     android:contentDescription="@string/content_desc_message_icon" 
     android:src="@drawable/ic_action_picture"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/senderLabel" 
     android:layout_centerVertical="true" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:layout_toRightOf="@+id/messageIcon" 
     android:layout_toEndOf="@+id/messageIcon"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text=" created at: 03.17.15" 
     android:id="@+id/createdAtLabel" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/senderLabel" 
     android:layout_toEndOf="@+id/senderLabel"/> 
</RelativeLayout> 

這是這種佈局的結果:

enter image description here

+0

在線性佈局中嵌套相對佈局在此也不是必需的 – 3xplore

+0

它是r7v,否則我應用於RelativeLayout的邊距根本不起作用,並且在中間沒有空格列表視圖。 –

+0

這是你的列表視圖行佈局? – 3xplore

回答

1

保證金在視圖之外,ListView默認使用AbsListView.LayoutParams,它不包括任何保證金支持,只是高度和寬度,這就是爲什麼它只是忽略邊緣的參數值。嘗試使用填充替代相對佈局

+0

我知道了,所以把相對佈局作爲容器放在另一個佈局中可以讓我們使用邊距,如果我是正確的。謝謝你的回答。 –