【问题标题】:Android: margin/padding for the ListView without having the margin applied to the header?Android:ListView 的边距/填充,而没有将边距应用于标题?
【发布时间】:2012-12-27 07:32:43
【问题描述】:

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

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

EDIT1:更多信息

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

编辑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>

由于这篇文章的篇幅,我在这里删除了无用的布局属性。

【问题讨论】:

  • 对所有项目平等地应用填充,并且不对标题视图应用填充。
  • 我认为您的意思是向 listItem 布局添加边距。它不起作用。
  • 我的意思是为每个列表项布局项添加填充。并且不要在 Header 中添加相同的内容。
  • 您的意思是以编程方式为适配器的列表视图中的每个项目添加填充?
  • 如果您添加项目布局代码,那么我可以建议。我建议您不要以编程方式更改项目布局

标签: android android-layout android-listview


【解决方案1】:

经过大量挖掘后,我发现将 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>

【讨论】:

  • 您可以移除外部的 LinearLayout 而不是 android:layout_marginRight="16dp" android:layout_marginLeft="16dp" 放置 android:paddingLeft="16dp" android:android:paddingRight="16dp" 或至少使用外部的 FrameLayout 而不是 LinearLayout
【解决方案2】:

在您的标题部分,添加以下标签:

android:layout_alignParentLeft="true"

这将解决您的问题。

【讨论】:

  • 我的标题是一个 xml,我以编程方式膨胀并通过 listView.addHeaderView(headerView) 添加列表。 header 绑定到 listView。所以改变列表视图的边距显然会改变标题。我在 header.xml 上尝试了你的建议,但它不起作用。
  • 您在根目录上使用什么布局?
  • 只有在列表视图外使用相对布局时才有效。
  • 抱歉,这不是解决方案。问题是列表视图的布局参数不能应用于其标题。用更多信息编辑了问题
  • 在您的 list_item.xml 而不是 root_list.xml 中应用边距。它肯定会起作用。
【解决方案3】:
// 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;

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

如果第三个参数为false,则只有maketemp.setLayoutParams(params),而params是root生成的!!和温度是作为回报....

如果attachToRoottrue,root会执行root.addView(temp, params),然后root作为return..

如果你只是做LayoutInflater.from(this).inflate(R.layout.popup_choose_type, null);,并且如果你已经设置了一些属性为paddingBottom,你会感到惊讶的是它不起作用!!因为丢失LayoutParams

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

【讨论】:

    【解决方案4】:

    您可以通过在列表视图的布局文件中设置边距来实现,然后您将得到它的输出。

    【讨论】:

    • 在列表视图布局文件中添加边距让我来到这里。我认为你不明白这个问题。
    • 你能解释一下垃圾,我会帮你吗?
    猜你喜欢
    • 2014-03-05
    • 2012-07-27
    • 2015-06-09
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多