2011-01-31 39 views
53

是否有人知道如何在Android平臺的TextView中包裝文本。即如果textview中的文本超過了屏幕長度,它應該顯示在第二行。如何在android中的textview中包裝文本

我已經搜索並嘗試了以下內容:

android:scrollHorizontally="false", 
android:inputType="textMultiLine", 
android:singleLine="false" 

但沒有工作..

任何人都可以建議我怎麼能做到這一點.. 我是比較新的Android平臺..

+3

默認情況下,TextView應該自動換行。你可以發佈你的整個佈局? – danh32 2011-01-31 16:28:29

+0

任何機會是表格佈局中的Textview?如果有的話,我爲你解答。 – JohnFx 2011-02-22 00:55:13

回答

0

你需要在ScrollView中添加你的TextView,像這樣:

<ScrollView android:id="@+id/SCROLL_VIEW" 
android:layout_height="150px" 
android:layout_width="fill_parent"> 

<TextView 
    android:id="@+id/TEXT_VIEW" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" /> 
</ScrollView> 
+4

謝謝您的回覆但我的目標是在文字超過行長時不會放置水平卷軸。 – Sameer 2011-02-01 02:25:33

9

您必須使用2個參數:

  • android:ellipsize="none":文本不削減TextView的寬度

  • android:scrollHorizontally="false"文字環繞在必要時

+3

這不適合我。 (FWIW,我在TableLayout的TableRow中有一個TextView。) – aij 2014-06-08 01:19:24

0

自嘆不如儘可能多的行 - 我在代碼中創建了我的TextView,並且它包裝 - 儘管我沒有設置任何東西,除了標準的東西 - 但請親自看看:

LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT); 
childParams.setMargins(5, 5, 5, 5); 

Label label = new Label(this); 
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label"); 
label.setLayoutParams(childParams); 

正如您從參數定義中可以看到的,我使用的是LinearLayout。類Label只是簡單地擴展了TextView - 除了設置字體大小和字體顏色外,沒有做任何事情。

在仿真器(API級別9)中運行它時,會自動將文本換行爲3行。

+0

請注意此代碼的使用`LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);`。最有可能的是,你認爲包裝是「automagic」 – nemesisfixx 2012-01-13 21:18:04

5

這應該解決您的問題:android:layout_weight="1"

+0

這只是導致TextView擴展到儘可能多的空間,這與將Height設置爲`match_parent`相同。與OP想要的不太一樣。 – 2016-11-11 16:47:10

+0

在TableLayout的情況下,`layout_weight =「1」`爲我解決了這個問題。但是,包含TextView的TableRow將`layout_height`設置爲特定的值。也許會對某人有用 – kit 2017-08-29 19:02:07

43

對我來說,這個問題只發生在Android 4.0 <

的我所使用的參數組合爲:

android:layout_weight="1" 
android:ellipsize="none" 
android:maxLines="100" 
android:scrollHorizontally="false" 

的MAXLINES計數似乎是做了我的TextView包隨機最後一塊。

+7

我現在知道它的一箇舊線程,但認爲我會張貼我的發現。我只用了'android:maxLines',然後它包裹了我的文本。 Cheer @Guykun – StuStirling 2012-10-10 16:02:48

+0

同樣,這等於簡單地將TextView的高度設置爲「match_parent」,這與使TextView的高度展開以包含額外行不同。 – 2016-11-11 16:52:09

5

好吧,傢伙事實是在中間的某個地方,你必須從父母的觀點和孩子的角度看問題。下面的解決方案僅當微調模式= dialog不管Android版本(那裏沒有問題..在VD測試了它和慾望與Android => 2.2):

  1. 設置你微調的(父)模式,如:

    android:spinnerMode="dialog" 
    
  2. 設置的TextView的(子自定義視圖)屬性:

    android:layout_weight="1" 
    android:ellipsize="none" 
    android:maxLines="100" 
    android:scrollHorizontally="false" 
    

我希望這也適用於你。

+0

嘗試了很多事情,使其在'下拉'模式下工作,但沒有任何工作。現在安置在'對話'模式。 – Mangesh 2015-08-09 15:31:22

1

通過將android:maxEms設置爲給定值以及android:layout_weight="1"將導致TextView在達到ems的給定長度時進行換行。

0

我使用的是Android 2.2,如果超過屏幕,我的textview將自動轉到下一行。

如果您希望在屏幕結束之前將文本轉到下一行,只需添加(只需輸入您自己的dp值)即可。如果您在文本右側有圖片,這將很有用。

android:layout_marginRight="52dp" 
24

對於其中TextViewTableLayout內的情況下,溶液是設置android:shrinkColumns="1"TableLayout。 (替換1與您要包裝的TextView的列號相同。(0索引))

AFAICT,TextView上不需要其他屬性。

對於其他情況,請參閱此處的其他答案。

FWIW,我最初得到它的排序工作與

<TextView 
    android:id="@+id/inventory_text" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:width="0dp" 

但導致在該對話框的底部,一些額外的空白空間是所有英寸

+0

這就是我一直在尋找的!否則我的話被切斷了 – 2017-08-31 08:36:32

-1

我知道它的晚,但它可能有助於其他人。只需設置寬度爲0dp

<TextView 
     android:layout_width="0dp" 
     .../> 
+1

這必須是一個笑話,對吧? – 2016-11-11 16:49:33

0

嘗試@ Guykun的方法並確保父寬度未設置爲包裝內容。這是我錯過的東西。

-1

其實很簡單。只需編寫: +「\ n」無論你想要一個wordwrap

2

在Android Studio 2.2.3的inputType屬性下有一個名爲textMultiLine的屬性。選擇這個選項爲我解決了一個類似的問題。我希望有所幫助。

0

我有同樣的問題。以下更改使其工作 -

android:layout_width="wrap_content" 

省略號,maxLines或layout_weight - 都沒有任何區別。 注 - 父寬度也設置爲wrap_content。

8

約束佈局

<TextView 
android:id="@+id/some_textview" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 

app:layout_constraintLeft_toLeftOf="@id/textview_above" 
app:layout_constraintRight_toLeftOf="@id/button_to_right"/> 
  • 確保您的佈局寬度爲零
  • 左/右約束定義WRAP_CONTENT的
  • 佈局高度允許膨脹向上/向下。
  • 設置android:maxLines="2"以防止垂直擴展(2只是例如)
  • 橢圓是概率。 ,最大線android:ellipsize="end"

0dp寬度是一個好主意允許左/右約束,以確定你的widget有多寬。

設置左/右約束設置您的小部件的實際寬度,文本將在其中進行包裹。

Constraint Layout docs

0

剛在RecyclerView內部佈局內一個TextView工作。我有文字被切斷,例如,Read this message,我看到:Read this。我嘗試在TextView上設置android:maxLines="2",但沒有任何更改。但是,android:lines="2"在第一行產生了Read this,在第二行產生了message

相關問題