如果 textView 所在的行太长,则换行一段文本

Wrapping a block of text if the line that textView sits on gets too long

我正在寻找满足此要求的最佳方法。如果在其他地方得到了回答,我深表歉意,我找不到解决这个看似简单的问题的方法。

列表中有一行文本如下所示:

项目标题 - (5 项目)

如果项目的标题变得太长,它就会换行,这是预料之中的。但是,它不应在“5”和 "items" 之间换行。如果需要换行,那么整个“(5 项)”应该换行到下一行。标题的文字应该是椭圆形的。

我似乎找不到创建适用于此的布局的方法。

This 可能会有帮助。您可以使用 android:ellipsize 属性定义文本的哪一部分将被截断。

在你的情况下,根据链接的答案,我会使用 android:ellipsize="middle"android:ellipsize="marquee",这使得文本幻灯片可以阅读。

虽然这实际上并不能满足您的要求(自动转到第二行的项目数量),但我认为这可能是更好的解决方案。

查看官方信息here


我想到的另一件事是使用两个不同的 TextViews,一个用于标题,另一个 (right-aligned?) 用于项目计数。我会给第一个 android:ellipsize="end"android:layout_width="wrap_content" 给后者。


您还可以在标题下方找到一个计数 TextView,如果有足够的空间,它会附加到标题。

<LinearLayout 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:id="@+id/container"
    android:orientation="vertical" >

    <TextView android:id="@+id/text1"
        android:text="a very very very long title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end" />


    <TextView android:id="@+id/text2"
        android:text="(5 items)"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

并且在您的代码中您可以检查宽度:

View container = findViewById(R.id.container);
TextView tv1 = (TextView) findViewById(R.id.text1);
TextView tv2 = (TextView) findViewById(R.id.text2);


String HEADER = tv1.getText();
String COUNT = tv2.getText();

int widthHeader = tv1.getWidth();
int widthCount = tv1.getWidth();
int widthAvailable = container.getWidth();

if (widthAvailable - widthHeader > widthCount) {
    tv1.setText(HEADER + " - " + COUNT);
    tv2.setText("");
}

TextUtils 类具有名为 ellipsize

的方法

public static CharSequence ellipsize (CharSequence text, TextPaint p, float avail, TextUtils.TruncateAt where) Added in API level 1

Returns the original text if it fits in the specified width given the properties of the specified Paint, or, if it does not fit, a truncated copy with ellipsis character added at the specified edge or center.

下面是我们如何使用它。假设 "Title of the item - (5 items)" 是全文,(5 项)是后缀,textView 是包含文本的文本视图。

String ellipsizedText = TextUtils.ellipsize(fullText, textView.getPaint(), fullTexViewtWidth ,TextUtils.TruncateAt.END);

现在要么将文本省略,要么不省略。我们可以通过检查 ellipsizedText 中是否存在后缀来检查这一点。 如果它是椭圆形的(删除后缀),我们应该再次调用 ellipsize 函数,但这次减少宽度,因为我们想为我们的后缀字符串保留 space 并删除后缀,因为我们是单独添加它的。因此,新电话将是

ellipsizedText = TextUtils.ellipsize(removedSuffixText, textView.getPaint(), reducedWidth ,TextUtils.TruncateAt.END);

最后我们将textView的文本设置为ellipsizedText+suffix;

我们需要找出的值很少

  1. fullTexViewtWidth - 这可能很棘手,因为我们没有为文本视图指定特定的宽度。假设我们将宽度设置为匹配父级,那么我们可以通过视图树观察器来实现它。按照答案 here
  2. reducedWidth - 这更加棘手。为了计算这个,我们需要找到后缀占用的宽度并从 fullTexViewtWidth 中减去它。 This answer 解释如何找到后缀占用的宽度

    final float densityMultiplier = getContext().getResources().getDisplayMetrics().density; final float scaledPx = 20 * densityMultiplier; paint.setTextSize(scaledPx); final float size = paint.measureText("sample text");

恐怕只有当 maxLines 为 1 且宽度设置为 textView 的 matchparent 时才有效。

从你的问题中我可以了解到

1) It does not matter how long the title text is,it can wrap according to its length.

2) Your text "(5 items)" should not break, it should stays together. What ever
   is the length of your TITLE.

如果以上2点是correct.Then简单的解决方法就是这个

 Just replace your String "(5 items)"

 String "(5&#160;items)"

注:

Here, '&#160' is character code of 'SPACE'. This bind's your string as one 
String and stays together.

希望这能解决您的问题。

您是否尝试过使用两个 textView 的相对布局,一个用于标题,另一个用于 "label"?使用左侧的相对布局:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container" >

<TextView android:id="@+id/text1"
    android:text="a very very very long title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:toLeftOf="@+id/label" />


<TextView android:id="@+id/label"
    android:text="(5 items)"
    android:singleLine="true"
    android:layout_alignParentRight="true" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

您也可以使用 linearLayout 权重,这将使文本始终适合某个比例(在本例中,标题为 80%,标签为 20%):

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight_sum="100"
android:orientation="horizontal"
android:id="@+id/container" >

<TextView android:id="@+id/text1"
    android:weight="80"
    android:text="a very very very long title"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />


<TextView android:id="@+id/label"
    android:text="(5 items)"
    android:singleLine="true"
    android:weight="20"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>