如何停止 Android TextView,其中有大量文本从右侧挤出一个文本?
How do I stop an Android TextView with lot of text squeezing out one to its right?
我试图让两个 TextView 并排居中——允许它们随着数据的增长而增长。当文字 太大 时,我将其省略。
我遇到的问题是,如果其中一个框包含大量文本,它会挤压另一个框。我想要的是小的要充分展示,大的要多挤一点。
目前,当我在两个框内有如下文字时
- 一长串单词将另一个 TextView 推出
- Foobar
两者都被挤压了。
在这种情况下,我希望 "Foobar" 完全显示,较长的 TextView 放弃一些 space---例如
...the other TextView out Foobar
当我得到现在是
...that pushes the other TextView out ...ar
如果两个文本框的大小相似,我希望它们都得到公平的份额。
下面是我的尝试。
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="A big long list of words that pushes the other TextView out" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="Foobar" />
</LinearLayout>
我还尝试了一个相对输出(见下文)---使用最小宽度---但这只是将最右边的 TextView 完全挤出了父视图!
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="A big long list of words that pushes the other TextView out" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:minWidth="100dp"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="Foobar" />
</RelativeLayout>
有人知道怎么做吗?
看看 Google 的 FlexboxLayout。我认为它可能具有您流动 TextView
所需的能力。您可以找到一些文档 here and a tutorial here.
FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.
更新:
我不确定 FlexboxLayout
是否是解决方案。这是另一个使用 TableLayout
和 TableRow
的例子。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<!-- TableLayout is needed to specify shrinkColumns. Ignore "Useless parent" error. -->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="start"
android:maxLines="1"
android:text="A big long list of words that pushes the other outwords that pushes the other TextView out"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:maxLines="1"
android:text="Foobar"
android:textSize="20sp" />
</TableRow>
</TableLayout>
我试图让两个 TextView 并排居中——允许它们随着数据的增长而增长。当文字 太大 时,我将其省略。
我遇到的问题是,如果其中一个框包含大量文本,它会挤压另一个框。我想要的是小的要充分展示,大的要多挤一点。
目前,当我在两个框内有如下文字时
- 一长串单词将另一个 TextView 推出
- Foobar
两者都被挤压了。
在这种情况下,我希望 "Foobar" 完全显示,较长的 TextView 放弃一些 space---例如
...the other TextView out Foobar
当我得到现在是
...that pushes the other TextView out ...ar
如果两个文本框的大小相似,我希望它们都得到公平的份额。
下面是我的尝试。
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="A big long list of words that pushes the other TextView out" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="Foobar" />
</LinearLayout>
我还尝试了一个相对输出(见下文)---使用最小宽度---但这只是将最右边的 TextView 完全挤出了父视图!
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="A big long list of words that pushes the other TextView out" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:minWidth="100dp"
android:maxLines="1"
android:ellipsize="start"
android:textSize="20dp"
android:text="Foobar" />
</RelativeLayout>
有人知道怎么做吗?
看看 Google 的 FlexboxLayout。我认为它可能具有您流动 TextView
所需的能力。您可以找到一些文档 here and a tutorial here.
FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.
更新:
我不确定 FlexboxLayout
是否是解决方案。这是另一个使用 TableLayout
和 TableRow
的例子。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<!-- TableLayout is needed to specify shrinkColumns. Ignore "Useless parent" error. -->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="start"
android:maxLines="1"
android:text="A big long list of words that pushes the other outwords that pushes the other TextView out"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:maxLines="1"
android:text="Foobar"
android:textSize="20sp" />
</TableRow>
</TableLayout>