本文介紹了如果單詞對于 textview 來說太長,則強制下一個單詞換行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個 TextView
,它在以編程方式填充時不會正確地換行.
I have a TextView
, that when filled programmatically will not properly line break on words.
這是正在發(fā)生的事情:
| Hi I am an examp |
| le of a string t |
| hat is not break |
| ing correctly on |
| words |
預(yù)期輸出:
我想要這個:
| 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),調(diào)用measureText
on油漆.如果結(jié)果長度比您的 TextView 的可用寬度長,請在新單詞前添加
.重置數(shù)據(jù)并重復(fù)上述步驟.
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.
這篇關(guān)于如果單詞對于 textview 來說太長,則強制下一個單詞換行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!