如果单词对于 textview 来说太长,则强制下一个单词换行 [英] Force next word to a new line if the word is too long for the textview

查看:59
本文介绍了如果单词对于 textview 来说太长,则强制下一个单词换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextView,它在以编程方式填充时不会正确地换行.

I have a TextView, that when filled programmatically will not properly line break on words.

这是正在发生的事情:

| Hi I am an examp |
| le of a string t |
| hat is not break |
| ing correctly on |
| words            |

预期输出:

我想要这个:

| Hi I am an       |
| example of a     |
| string that is   |
| breaking         |
| correctly on     |
| words            |

Java:

String mQuestion = "Hi I am an example of a string that is breaking correctly on words";
mTextView.setText(mQuestion);

XML:

<LinearLayout
    android:id="@+id/questionContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/questionHeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/header_bg"
        android:paddingBottom="10dp"
        android:paddingLeft="25dp"
        android:paddingTop="10dp"
        android:text="@string/category_hint"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".05" />

        <!-- THIS ONE WILL NOT WRAP !-->
        <TextView 
            android:id="@+id/questionHolderTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".9"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:text="@string/question_holder"
            android:singleLine="false"
            android:scrollHorizontally="false"
            android:ellipsize="none"
            android:textSize="20sp" />

        <Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".05" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="5dp"
        android:background="@color/black" />
</LinearLayout>

<LinearLayout
    android:id="@+id/answerContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="25dp"
    android:gravity="center" >

    <Button
        android:id="@+id/nextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:text="@string/next" />
</LinearLayout>

推荐答案

首先你可以使用 TextView.getPaint(),然后每次添加一个新词(Hi, I, am, etc),调用measureText on油漆.如果结果长度比您的 TextView 的可用宽度长,请在新单词前添加 .重置数据并重复上述步骤.

First you can get the text paint using TextView.getPaint(), then each time you add a new word(Hi, I, am, etc), call measureText on the paint. If the result length is longer than the available width of your TextView, add a before the new word.Reset the data and repeat the steps.

这篇关于如果单词对于 textview 来说太长,则强制下一个单词换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆