【问题标题】:How to set the spacing in listview item如何在列表视图项中设置间距
【发布时间】:2016-05-16 17:02:25
【问题描述】:

我正在尝试制作这样的列表视图: Listview I want

我尝试了以下方法:

  1. 通过在列表视图中设置它但它不起作用

    android:divider="@android:color/transparent"
    android:dividerHeight="10.0sp"
    
  2. 通过在 textview 布局中设置 marginTop 但它也不起作用。

所以有人可以帮助我如何获得所需的输出。

我已经尝试过thisthis,但这些都不起作用。

这是我的自定义列表视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
    android:layout_width="8dp"
    android:layout_height="wrap_content"/>
<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/imageView"
    android:layout_margin="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_margin="5dp"
    style="@style/AppTheme.Text"
    />
<View
    android:layout_width="8dp"
    android:layout_height="wrap_content"
    />
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="8dp"
    android:background="#fff"/>
</LinearLayout>

我得到了这个:

【问题讨论】:

  • 您能否发布有关您的 ListView 的更多详细信息?它有自定义视图(xml 文件)吗?
  • 我已经更新了@RosárioPereiraFernandes 的问题
  • 不要将 SP 与这些属性一起使用。它用于字体大小。更多信息请查看 - stackoverflow.com/questions/2025282/…
  • 我试过那个,但我主要使用 dp
  • 恕我直言,您应该使用 RecyclerView with CardView 而不是 OLD ListView。那只会给你YOUR DESIRED LISTVIEW

标签: android android-layout listview


【解决方案1】:

好的,用这个。它应该完全如你所愿。随意更改边距和填充。

在您的自定义 ListView 布局中,使用此 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#009688">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/rectangle">

        <ImageView
            android:id="@+id/iv_imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="5dp"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tv_hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_toRightOf="@+id/iv_imageView1"
            android:textColor="@android:color/black"
            android:textSize="16sp" />
    </RelativeLayout>

</RelativeLayout>

现在,在您的可绘制文件夹中创建一个新的 xml,将其命名为矩形。将下面的代码粘贴到其中。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#ff207d94" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="8dp" />
    <solid android:color="#ffffffff" />
</shape>

半径定义了你想要的矩形的曲线,你可以随意改变它。

在适配器中使用自定义 ListView 布局。这应该有效。

编码愉快……干杯!!

【讨论】:

    【解决方案2】:

    您可以调整每个项目的 ListView 以喜欢这个示例:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="Large Text" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="8dp" />
    
    </LinearLayout>
    

    这是结果,只是在父布局中添加一些边距并放置一个空视图以占用 8dp 空间,以便视图的下一个项目将比第一个项目相距 8dp,依此类推。

    【讨论】:

    • 它有效,但它不是我想要的我也尝试在两侧设置视图,但它不起作用
    • 你的意思是你把它放在 textview 之前和之后?结果如何?
    • 第二张图片是输出我得到左右视图不起作用我设置了背景也它只适用于最后一个视图
    • 尝试在 Activity 的布局 xml 中为 ListView 添加 4dp 边距