如何在listview

时间:2016-09-23 14:03:58

标签: android listview textview

下面提到的布局是ListView中一个项目的模型。如您所见,三个TextView与每个TextView分开,因为每个TextView占据整个行空间的.3

问题是,当我向ListView添加项目时,我发现三个TextView只是相互关联。例如,我们假设我要将项目添加到ListView包含以下内容:

Adam USA M

我希望在屏幕上看到一行,每个textView分隔一些空格,但会发生以下情况:

AdamUSAM

为什么会发生这种情况以及如何解决?

model_view

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

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="Name"/>

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="Address: "/>

    <TextView
        android:id="@+id/tvGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="gender: "/>
</LinearLayout>

更新

现在我将布局更改为如下,但问题仍然存在,即三个文本视图在没有间距的情况下相互拼接。

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Name: "
        android:focusable="false"/>

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Address: "
        android:focusable="false"/>

    <TextView
        android:id="@+id/tvGender"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="gender: "
        android:focusable="false"/>
</LinearLayout>

屏幕截图

enter image description here

5 个答案:

答案 0 :(得分:2)

您需要指定LinearLayout的方向,例如android:orientation="horizontal"然后将权重替换为1而不是0.3,并确保TextView宽度为0dp而不是wrap_content

答案 1 :(得分:1)

为每个TextView添加layout_marginpadding

答案 2 :(得分:0)

使用属性      机器人:layout_margin = “10dp”

答案 3 :(得分:0)

你的listView xml中的

添加:

android:dividerHeight="8 dip"

或您想要的任何价值。

答案 4 :(得分:0)

我建议从layout_weight中删除TextViews,为其定义一个特定的height并添加填充。添加边距可能会导致TextViews退出ListView