【问题标题】:How to make a TextView reduce its width if text is wrapped and moved to next line?如果文本被换行并移动到下一行,如何使 TextView 减小其宽度?
【发布时间】:2016-07-13 08:11:07
【问题描述】:

在 TextView 中,当单词的长度大于宽度可以容纳时,它会将文本换行并将其移动到下一行。但是,即使右侧有空白区域,TextView 也不会换行它自己的宽度。

如何让 TextView 在包裹文本时减小其宽度?

这是 TextView :

<TextView
    android:id="@+id/userMessageTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="40dp"
    android:layout_marginTop="2dp"
    android:autoLink="web"
    android:background="@drawable/textview_design"
    android:padding="8dp"
    android:text="some text"
    android:textColor="#333333"
    android:textSize="17sp" />

还有,textview_design.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#EEEEEE" />
    <corners
        android:radius="@dimen/chat_message_text_corner_radius" />
</shape>

【问题讨论】:

  • 你在使用android:layout_width="wrap_content"吗?
  • 在你的 xml 中为 textView 制作你的 android:layout_width=wrap_content
  • 显示这个文本视图的 xml 文件
  • @user13 是的,textview的布局宽度是wrap_content。
  • @Vickyexpert 我已经添加了 textview xml

标签: android textview word-wrap


【解决方案1】:

正如您在示例中看到的,当有新行 (\n) 分隔内容时,TextView 可以完美地包装其内容。

所以解决方案很简单,计算新行的位置并将它们插入到您的字符串中!

我使用measureText的方法来计算当前文本的宽度。如果它小于最大宽度,我会尝试在该行上添加更多文本。当它达到maxWidth时,插入一个新行!

String message = "This is a long message that will be displayed on multiple lines";
int textViewContentMaxWidth = 600; // you need to calculate that value, for me it was screenWidth - 40 dp (16dp margins on each side and 4 dp padding on each side)

String[] sections = message.split(" ");
String formattedMessage = ReplaceBreakWordByNewLines(sections, tv.getPaint(), textViewContentMaxWidth)
tv.setText(formattedMessage);

--

public static String ReplaceBreakWordByNewLines(String[] _texts, Paint _paint, int maxWidth)
{
    String formattedText = "";

    String workingText = "";
    for (String section : _texts)
    {
        String newPart = (workingText.length() > 0 ? " " : "") + section;
        workingText += newPart;

        int width = (int)_paint.measureText(workingText, 0, workingText.length());

        if (width > maxWidth)
        {
            formattedText += (formattedText.length() > 0 ? "\n" : "") + workingText.substring(0, workingText.length() - newPart.length());
            workingText = section;
        }
    }

    if (workingText.length() > 0)
        formattedText += (formattedText.length() > 0 ? "\n" : "") + workingText;

    return formattedText;
}

【讨论】:

  • 这是一个有趣的解决方案。不知道为什么我没有想到这一点。无论如何,这个问题是很多项目之前的问题,我还没有真正考虑过。这对你有用吗?另外,什么是 textViewContentMaxWidth?
  • 另外,你有没有看过这个:(支持库 26.0 的一部分)androidkt.com/autosizing-textviews
  • 我看到了支持库。但我和你一样想要一个更小的文本视图,因为我不想背景填充整个空间。
  • 是的,它有效。我添加了截图。我将 textViewContentMaxWidth 计算为屏幕宽度减去容器视图边距(每侧 16dp)和 TextView 填充(每侧 4dp)。
猜你喜欢
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多