【问题标题】:ListView header - different marginListView 标题 - 不同的边距
【发布时间】:2015-12-02 06:05:36
【问题描述】:

我正在创建一个带有列表视图和其他一些视图的布局。我想要完整的布局与列表一起滚动,所以我将其他视图添加为标题。问题是我的列表视图有一些边距也应用于标题。

我应该改变什么以使标题看起来像这样-

列表视图

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

    <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:scrollbars="none"
            android:id="@+id/lvProfileAddresses"
            android:layout_gravity="center_horizontal"
            android:divider="@color/transparent"
            android:dividerHeight="10dp"
            android:paddingBottom="10dp"
            android:clipToPadding="false"
            android:cacheColorHint="@color/background_color"
            android:fadingEdgeLength="3dp"

            />
</LinearLayout>

标题

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/transparent"
              >
    <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/white"
              android:text="My Addresses"
              android:textSize="20sp"
              android:padding="10dp"
              android:textColor="@color/black"
            />
    <Button android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add New Address"
            android:drawableRight="@drawable/small_list_arrow"
            android:layout_margin="5dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:background="@drawable/linearlayout_shadow"/>

</LinearLayout>

如何让ListView的header有不同的margin?

【问题讨论】:

  • 如何将标题添加到 Listview?
  • @MysticMagic 膨胀视图并使用listview.addHeaderView()

标签: android listview android-listview margin


【解决方案1】:

我认为ListView中最好不要添加Header,保留单独的Header。也许你可以使用include

类似:

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

    <include
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/header" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:scrollbars="none"
        android:id="@+id/lvProfileAddresses"
        android:layout_gravity="center_horizontal"
        android:divider="@color/transparent"
        android:dividerHeight="10dp"
        android:paddingBottom="10dp"
        android:clipToPadding="false"
        android:cacheColorHint="@color/background_color"
        android:fadingEdgeLength="3dp" />
</LinearLayout>

您只需要在顶部的 ListView 中进行一次。所以我认为这将是一个更好的方法。希望对您有所帮助。

编辑:

由于您需要在 ListView 中保留 header 以便将其与 ListView 项目一起滚动,您可能可以采用其他方式:

  1. 从整个 ListView 中移除边距属性。

  2. 将边距应用于 ListView 的 row_item.xml。为此,您需要将 row.xml 的外部容器包含在另一个父容器中,例如 LinearLayout。所以保证金会起作用。

希望这会有所帮助。

【讨论】:

  • 但这不会使标题无法与列表一起滚动吗?我希望我的完整布局与列表一起滚动。
  • 那么我想你可以从整个列表视图中删除边距并将边距给 list_row.xml @RohanKandwal
  • @RohanKandwal 填充?
  • 填充正在工作,但它会使 list_row.xml 中的视图向内移动,它不会缩小整行,即该行仍会拉伸到完整宽度。
  • @RohanKandwal 如果您将行布局放在另一个父 LinearLayout 的一侧,并将边距应用于行布局,那应该可以。你试过这样吗?
【解决方案2】:

如果你想用 header 来做,你需要从你的列表视图中删除边距属性,因为它会影响所有子视图并为你的 listItem 添加相同的边距。

【讨论】:

  • 为 listItem 添加边距不起作用。当从列表视图中删除边距时,子视图会扩展为全宽。
  • 对不起。这是因为 ListView.LayoutParams 不是 MarginLayoutParams 的子项。因此,您可以将列表项放在具有透明背景的框架布局中,并为该框架布局设置填充
【解决方案3】:

我最近遇到了同样的问题,从我在平台上看到的情况来看,有些人仍在为此苦苦挣扎。 根据我的研究,列表视图看起来会覆盖页眉边距和填充。

解决办法是:

  1. 将您的标题视图组放入另一个视图组中 android:layout_height="wrap_content" 和 android:layout_width="match_parent"。
  2. 然后将所需的任何边距应用到内部视图组。

在您的情况下,标题布局如下所示:

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

    <LinearLayout 
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop ="40dp" //For Example
              android:layout_marginRight="20dp" 
              android:layout_marginEnd="20dp"
              android:background="@color/transparent">

            <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/white"
              android:text="My Addresses"
              android:textSize="20sp"
              android:padding="10dp"
              android:textColor="@color/black"/>

              <Button android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Add New Address"
              android:drawableRight="@drawable/small_list_arrow"
              android:layout_margin="5dp"
              android:paddingTop="5dp"
              android:paddingBottom="5dp"
              android:background="@drawable/linearlayout_shadow"/>

      </LinearLayout>
</LinearLayout>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2016-02-20
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多