【发布时间】:2018-05-13 04:32:58
【问题描述】:
我必须在TextViews 中表示一些短句。我使用recyclerView 和GridLayoutManager 来做到这一点,并将每个句子设置在网格每个单元格上的TextView 上。问题是我希望网格的每个单元格根据TextView 中的文本更改其宽度。我的意思是,一个长句子可以占据整个屏幕宽度,或者如果句子很短,它们必须在同一行。
我也尝试使用GridView,但我也无法获得想要的结果。
知道如何获得它吗?
编辑
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/unselected_term"
android:padding="10dp">
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/term_text"
android:gravity="center"
android:textSize="18sp"/>
</LinearLayout>
编辑 2
这是布局应该是什么样子的示例。每个元素的长度并不总是相同的。所以我需要一个布局,让较大的元素比较短的元素具有更大的宽度。
【问题讨论】:
-
你能把你的xml贴在这里吗?您用于 recyclerview 项目的布局
-
@Umair 代码已添加 :)
-
我已经更新了答案,请检查并告诉我它是否适合你。
标签: android gridview layout android-recyclerview