gpt4 book ai didi

android - Android 中自定义 ListView 的 TableLayout 问题

转载 作者:行者123 更新时间:2023-11-29 01:27:38 26 4
gpt4 key购买 nike

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

我想要像 this 这样的输出图片。 但是我的输出列表不同,有时列表中会丢失这些数字。 image2在 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继承自 LinearLayout weight的概念|对它也有效,根据它,必须将 layout_width 属性设置为 0dp 才能使 View 水平展开。

需要修改以下内容:

  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>

关于android - Android 中自定义 ListView 的 TableLayout 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33051384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com