ListView标头-不同的边距

罗汉·坎沃尔

我正在创建具有列表视图和其他一些视图的布局。我希望完整的布局与列表一起滚动,所以我将其他视图添加为标题。问题是我的列表视图也有一些页边距。

目前的样子

我应该更改什么,以便标题看起来像这样-

我想要的是

列表显示

<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的标题具有不同的边距?

神秘魔术师ϡ

我认为最好不要在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中以便将其与ListView项目一起滚动,因此可以采用其他方法:

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

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

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章