Android:ListView的边距/填充没有应用于标题的边距?

red*_*t13 4 android android-layout android-listview

是否可以为ListView设置边距/填充而不将边距应用于标题?我希望我的ListView与Google Now布局一样.除了这个小问题,我已经完成了所有工作.

问题:是否可以不将ListView的布局参数应用于其标题?

EDIT1:更多信息

紫色视图是listView的标题.我已经尝试在列表项布局中添加边距.这也不起作用.我需要的是一种方法,将边距应用于列表视图,以便它不会将边距应用于listView中的标头.

EDIT2:我的布局header.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/top_height"
        android:background="@color/top_item" />

    <View
        android:id="@+id/placeholder"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sticky_height"
        android:layout_gravity="bottom" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

root_list.xml

<LinearLayout>
<com.abhijith.CustomListView
    android:id="@android:id/list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_height="wrap_content" /></LinearLayout>
Run Code Online (Sandbox Code Playgroud)

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/textview_note"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview_note_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/></LinearLayout>
Run Code Online (Sandbox Code Playgroud)

由于这篇文章的篇幅,我已经删除了uselsess布局属性. 这是它的外观

red*_*t13 7

经过深入挖掘后,我发现将listView布局参数与其标题分离并且标题是listView的一部分是不可能的.因此,为了获得所需的保证金效果,经过多次调查,我发现在列表视图项中添加页面根布局的边距不起作用.因此,我在现有的根LinearLayout(嵌套的LinearLayout)中添加了另一个虚拟LinearLayout,并在嵌套布局中添加了子项.现在,内部布局的设置边距会产生所需的效果.

码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:background="@drawable/list_selector"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textview_note"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textview_note_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 你可以删除外部LinearLayout而不是`android:layout_marginRight ="16dp"android:layout_marginLeft ="16dp"`put`android:paddingLeft ="16dp"android:android:paddingRight ="16dp"`或者至少使用外部FrameLayout而不是LinearLayout (3认同)