我希望单元格的宽度根据内部文本的长度而变化。目前,width = 160dp是固定的,但如果我将wrapcontent

正如您所看到的,每个单元格都是根据数据的长度设置的,即使它们对应于同一列。
行数可以更改,但列数是4。
我希望遍历该列中的所有数据内容,并设置该列的宽度以适合该列中最长的文本。
RecyclerViewitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="160dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="@+id/tv_po"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:layout_width="160dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="@+id/tv_qty"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:layout_width="160dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="@+id/tv_material"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:layout_width="350dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="@+id/tv_info"
android:textStyle="bold"
android:gravity="center" />
</LinearLayout>
</LinearLayout>发布于 2021-06-16 11:34:22
我不确定您是否愿意这样做,RV adapter一次加载一个视图,直到RecyclerView满为止。每次适配器加载更大的View时,所有现有的Views都需要重新绘制。然后,如果从屏幕上删除了最大的视图,则需要刷新所有视图以匹配新的现有视图。这将使用大量资源来不断重新加载图像,并可能导致应用程序冻结。但是,如果你真的热衷于设置它,请查看以下问题。
How to set textview height for the largest string in android recyclerview?
发布于 2021-06-16 12:06:38
您可以使用芯片(材料设计)来满足您的需求。https://material.io/components/chips/android#using-chips
https://stackoverflow.com/questions/67995683
复制相似问题