2011-03-08 20 views
46

我顯示與行的TableLayout如下通過tablerow顯示在TextView中多行文字:Android和在

<?xml version="1.0" encoding="utf-8"?> 
<TableRow 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/one" 
      android:layout_marginLeft="10dip" 
      android:textColor="#B0171F" /> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/one" 
      android:id="@+id/two" 
      android:layout_marginLeft="10dip" 
      android:ellipsize="none" 
      android:singleLine="false" 
      android:scrollHorizontally="false" 
      android:maxLines="10" 
      android:textColor="@android:color/black" /> 

    </RelativeLayout> 

</TableRow> 

我打這一切我可以在這裏找到,可以想到的允許文本在多行上打包,但無濟於事:文本總是被強制爲一行,從屏幕上跑出。這可能很重要,我在這裏工作在TableRow中,而據我所知,這個網站上沒有對此進行處理。

那麼,我如何強制我的第二個TextView換行?

+0

TableRow就是這樣一行。我認爲你不應該嘗試在單一行上創建多行,這就是重點。那裏使用TableRow的目的是什麼? – Klaus 2011-03-08 09:37:11

+0

你嘗試刪除'android:singleLine =「false」'行嗎? – 2011-03-08 09:46:23

+0

剛剛在Eclipse中複製了您的代碼,並且文本正確包裝,正如我在圖形佈局中看到的。 (我剛加了'android:text =「(長文本)」'有一些文字可以顯示) – Adinia 2011-03-08 09:46:43

回答

92

如果它所在的列設置爲縮小,TextView將包裝文本。有時如果文本以「,...」結尾,則它不會完全換行,然後比正好n行更長一點。

下面是一個例子,id爲question TextView的將包裹:

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="*"> 
    <TableRow> 
     <TextView 
      android:id="@+id/question" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </TableRow> 
</TableLayout> 
+0

謝謝你。我會在適當的時候嘗試一下(我在這個項目上有幾件事需要注意),並希望接受一個答案。 – SK9 2011-03-22 04:58:51

+9

@All:'android:shrinkColumns =「0」'完全適合我,而不是我一直在嘗試的'android:shrinkColumns =「1」'。我不確定這是你的建議,但你的答案是堅實的,我的評論出現在這裏。他們一起回答了我的問題。 – SK9 2011-03-23 03:44:02

+0

@All:android:stretchColumns =「0」也是有用/更好的,看起來,如果(和我的情況一樣)表格只有一列,你想伸展來填滿屏幕。我的TableLayout XML在本頁的答案中給出(在下面的輸入時)。 – SK9 2011-03-23 12:31:30

3

也許嘗試如下:

myTextView.setText(Html.fromHtml("On " + stringData1 + "<br> at " + strinData2); 

我知道,看起來有點像一個「輪椅」的解決方案,但對我的作品,但我也編程填充文本。

13

爲了完整起見,這是怎麼了我的TableLayout在XML寫着:

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:shrinkColumns="0" 
    android:stretchColumns="0" 
    android:id="@+id/summaryTable"> 
</TableLayout> 

我後來填充此與TableRow s。

2

通過代碼也可以工作:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table); 
tablelayout.setColumnShrinkable(1,true); 

1是列的數目。