Android:ListView的边距/填充,而不将边距应用于标题?

Android:ListView的边距/填充,而不将边距应用于标题?,android,android-layout,android-listview,Android,Android Layout,Android Listview,是否可以在不将边距应用于页眉的情况下为ListView设置边距/填充?我希望我的ListView像Google Now布局一样。除了这个小问题,我什么都做得很好 问题:是否可以不将ListView的布局参数应用于其标题 编辑1:更多信息 紫色视图是listView的标题。 我尝试过在列表项布局中添加边距。这也不起作用。 我所需要的只是一种将边距应用于listview的方法,这样它就不会将边距应用于listview中的标题 编辑2: 我的布局 header.xml <FrameLayout

是否可以在不将边距应用于页眉的情况下为ListView设置边距/填充?我希望我的ListView像Google Now布局一样。除了这个小问题,我什么都做得很好

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

编辑1:更多信息

紫色视图是listView的标题。 我尝试过在列表项布局中添加边距。这也不起作用。 我所需要的只是一种将边距应用于listview的方法,这样它就不会将边距应用于listview中的标题

编辑2: 我的布局 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>

root_list.xml

<LinearLayout>
<com.test.CustomListView
    android:id="@android:id/list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_height="wrap_content" /></LinearLayout>

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>

由于这篇文章的长度,我已经删除了Useless布局属性。

您可以通过在listview的布局文件中指定边距来完成此操作,然后将其作为输出。

在标题部分,添加以下标记:

android:layout_alignParentLeft="true"

这将解决您的问题。

经过深入研究,我发现不可能将listView布局参数与其标题解耦,而标题是listView的一部分。因此,为了获得所需的边距效果,在仔细查看之后,我发现在列表视图项中向布局的根添加边距不起作用。因此,我在现有根LinearLayout(嵌套LinearLayout)中添加了另一个虚拟LinearLayout,并在嵌套布局中添加了子级。现在,将边距设置为内部布局可以提供所需的效果

代码:


这是
LayoutInflater
的代码,如您所见,如果添加
root
,它不是空的,是从root生成的
layoutParams

然后,如果第三个参数为false,则只有make
temp.setLayoutParams(params)
由root生成
params
!!而温度是作为返回

如果
attachToRoot
true,root将执行root.addView(temp,params),然后root作为return

如果你只是做
LayoutInflater.from(this).充气(R.layout.popup\u选择类型,空)
,如果您将某些属性设置为
paddingBottom
,那么您会惊讶地发现,它将不起作用!!因为丢失了布局参数

至于如何解决,你可以看到

对所有项目均应用填充,而不对页眉视图应用填充。我想您的意思是为列表项布局添加边距。这不起作用。我的意思是为每个列表项布局项目添加填充。不要在标题中添加相同的内容。您的意思是在适配器的listview中以编程方式向每个项目添加填充?如果您添加项目布局代码,我可以建议。我建议您更改项目布局,而不是编程。在listview布局文件中添加边距让我来到这里。我认为您不理解这个问题。请您进一步解释我,以便我能帮助您吗?我的标题是一个xml,我通过编程进行膨胀,并通过listview添加列表。addHeaderView(headerView)。标题已绑定到listView。所以改变listview的边距显然会改变标题。我在header.xml上尝试了您的建议,但它不起作用。您在根目录上使用的布局是什么?只有在listview之外使用相对布局时,它才会起作用。抱歉,这不是解决方案。问题是listview的布局参数不能应用于其标题。使用更多信息编辑了问题。请在list_item.xml而不是root_list.xml中应用边距。它肯定会起作用。您可以删除外部线性布局而不是
android:layout\u marginRight=“16dp”android:layout\u marginLeft=“16dp”
put
android:paddingLeft=“16dp”android:paddingRight=“16dp”
或者至少使用外部框架布局而不是线性布局
<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>
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, attrs, false);

ViewGroup.LayoutParams params = null;

if (root != null) {
    if (DEBUG) {
        System.out.println("Creating params from root: " +
                root);
    }
    // Create layout params that match root, if supplied
    params = root.generateLayoutParams(attrs);
    if (!attachToRoot) {
        // Set the layout params for temp if we are not
        // attaching. (If we are, we use addView, below)
        temp.setLayoutParams(params);
    }

// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
    root.addView(temp, params);
}

// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
    result = temp;
}

...
return result;