Android 中自定义 ListView 的 TableLayout 问题

Problems in TableLayout for custom ListView in Android

在我的 Android 应用程序中,我使用 JSON 从服务器检索数据。为了显示该数据,我使用了一个自定义 ListView,它有 3 个静态图像和两个 TextViews。数据显示在 ListView 中,但方式不同。

我想要像 this 图片那样的输出。 但是我的输出列表不同,有时列表中会丢失这些数字。 在 104 之后,缺少数字 105 和 106,然后缺少 110。我不知道为什么输出是这样的。下面是我的 xml 布局代码:

XML代码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2"
    android:shrinkColumns="1"
    android:layout_marginLeft="10dp">

    <TableRow
        android:layout_width="match_parent">
        <TextView
            android:id="@+id/SrNo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:text="SR NO."
            android:textColor="#000000"
            android:singleLine="true"
            android:layout_marginTop="10dp"/>

        <TextView
            android:id="@+id/Name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Expert Name Here."
            android:textColor="#000000"
            android:paddingBottom="5dp"
            android:gravity="center"
            android:layout_marginTop="10dp"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">

            <ImageView
                android:id="@+id/edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_view"
                android:paddingBottom="5dp"
                android:layout_marginTop="5dp"/>

            <ImageView
                android:id="@+id/view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_edit"
                android:paddingBottom="5dp"
                android:layout_marginTop="5dp"/>

            <ImageView
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_delete"
                android:paddingBottom="5dp"
                android:layout_marginTop="5dp"/>

        </LinearLayout>

    </TableRow>
</TableLayout>

您几乎已经参与了与@Sangeeta 的讨论。

因为 TableRow is inherited from LinearLayout the concept of weight 对它也有效,因此必须将 layout_width 属性设置为 0dp 才能使视图水平展开。

以下需要更改:

  1. 删除android:stretchColumns="0,1,2"
  2. 在使用 layout_weight 属性的地方设置 android:layout_width="0dp"

(注意:我只留下了问题的相关属性)

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

<TableRow
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/SrNo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"/>

    <TextView
        android:id="@+id/Name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25">

        <ImageView
            android:id="@+id/edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</TableRow>
</TableLayout>