如果单词对于textview来说太长,则强制下一个单词到新行

时间:2014-09-03 05:27:14

标签: java android xml textview

我有一个TextView,当以编程方式填充时,不能正确地换行。

当前输出:

这就是发生的事情:

| 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            |

爪哇:

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>

6 个答案:

答案 0 :(得分:8)

首先,您可以使用TextView.getPaint()获取文本绘画,然后每次添加新单词(嗨,我,上午等)时,在绘画上调用measureText。如果结果长度超过TextView的可用宽度,请在新单词前添加\n。重置数据并重复步骤。

答案 1 :(得分:2)

我最终使用

private void initView() {
        Paint paint = new Paint();
        float width = paint.measureText(mQuestion);
        int maxLength = 300; // put whatever length you need here
        if (width > maxLength) {
            List<String> arrayList = null;
            String[] array = (mQuestion.split("\\s"));
            arrayList = Arrays.asList(array);
            int seventyPercent = (int) (Math.round(arrayList.size() * 0.70)); // play with this if needed
            String linebreak = arrayList.get(seventyPercent) + "\n";
            arrayList.set(seventyPercent, linebreak);
            mQuestion = TextUtils.join(" ", arrayList);
            mQuestion.replace(",", " ");
        }
        mQuestionHolderTextView.setText(mQuestion);
    }

我测量字符串,将其转换为List,然后将其拆分为70%并创建一个新行。然后我将List转回String并删除逗号。只要该字不超过剩余线的30%,您就可以清楚地进行调整,否则进行相应的调整。

它既快又脏,但它对我有用。

答案 2 :(得分:1)

使用以下方法,您可以获取包装文本。

由于我没有设置android,所以我编写了一个Test类并从main调用了该方法。您需要传递textview宽度。我在这里过了14分。

    public class Test{


    public static void main(String[] args) {
        String wrappedText=wrapText(14);
        System.out.println(wrappedText);
    }

    public static String wrapText(int textviewWidth) {

        String mQuestion = "Hi I am an example of a string that is breaking correctly on words";


        String temp = "";
        String sentence = "";

        String[] array = mQuestion.split(" "); // split by space

        for (String word : array) {

            if ((temp.length() + word.length()) < textviewWidth) {  // create a temp variable and check if length with new word exceeds textview width.

                temp += " "+word;

            } else {
                sentence += temp+"\n"; // add new line character
                temp = word;
            }

        }

        return (sentence.replaceFirst(" ", "")+temp);

    }

}

输出 -

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

答案 3 :(得分:0)

感谢suitianshi,以下是我的解决方案。

GoogleSignInAccount lastSignedInAccount= GoogleSignIn.getLastSignedInAccount(context);
if(lastSignedInAccount==null){
// user has already logged in, you can check user's email, name etc from lastSignedInAccount
String email = lastSignedInAccount.getEmail();
}else{
// user is not logged in with any account
}

它工作和清洁代码。

答案 4 :(得分:0)

android:breakStrategy="simple"

将此添加到您的Textview中。它对我有用!

答案 5 :(得分:-1)

Android 不支持 TextView。尝试使用

value.replaceAll("\s", " ");

htmlText.replace("\n","
").replace("\s", " ")