如果布局中没有空格,则自动在下一行显示 TextView

如果 TextView 不适合当前行,我想自动将其移至下一行。例如,我在一个布局中有 20 个 TextView,并希望在其中设置客户名称。有些客户名称很长,有些则很小。根据该行中的可用空间,应该显示一个 TextView。如果没有空间,那么它应该自动移动到该布局中的下一行。

谁能帮我这个?


江户川乱折腾
浏览 170回答 3
3回答

尚方宝剑之说

标准的 Android 框架中没有任何东西可以让你这样做,但是有一个来自 Google 的非常好的外部库,叫做FlexboxLayout可以为你做这件事。您将使用 aFlexboxLayout作为所有TextViews 的父级,而不是例如 a LinearLayout,它将负责为您包装它们。<com.google.android.flexbox.FlexboxLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; app:flexWrap="wrap">&nbsp; &nbsp; <!-- add your 20 textviews here --></com.google.android.flexbox.FlexboxLayout>请注意,指定很重要,app:flexWrap="wrap"因为默认情况下不进行包装。我不确定为什么会这样,因为使用这个库的全部目的是为了包装,但是嘿。

蓝山帝景

听起来您正在尝试将某些文本视图芯片化。您将需要某种布局管理器来测量视图并确定如何显示它们。在 Github 上查看此内容或与 ContactChips 相关的任何内容都可能有用。芯片布局管理器

翻翻过去那场雪

使用高度作为 wrap_content 和宽度作为 match_parent 创建线性布局,然后在其中创建 Textview 并将其高度作为 wrap_content ,因此当您的内容增加时它会自动扩展。就这么简单。您可以查看下面的示例。&nbsp;<LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="4dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:singleLine="false"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/black"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="16sp" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </LinearLayout>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java