2012-10-05 33 views
2

我有TextViews,我希望它們在行中與換行符一樣。 TextViews是具有一些功能的對象。他們是用逗號分隔的單詞。我想用換行符的順序顯示它。android - 帶斷行的TextViews

這是我的代碼:

int i; 
String p = "one, two, three, four, five, six, seven, eight, ten"; 
String[] array = p.split(","); 

LinearLayout groupLL = new LinearLayout(this); 
LinearLayout.LayoutParams gLLP = new LinearLayout.LayoutParams(
    new ViewGroup.MarginLayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT)); 

LinearLayout.LayoutParams mlp = new LinearLayout.LayoutParams(
    new ViewGroup.MarginLayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT)); 
mlp.setMargins(0, 0, 10, 0); 

for (i = 0; i < array.length; i++) { 
    TextView newTextView = new TextView(this); 
    newTextView.setText(array[i]); 
    newTextView.setBackgroundColor(Color.RED); 
    newTextView.setSingleLine(true); 
    newTextView.setTextSize(20); 

    groupLL.addView(newTextView, mlp); 
} 

ll.addView(groupLL, gLLP); 

這是我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ScrollView02" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

    <RelativeLayout 
    android:id="@+id/RelativeLayout02" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout android:id="@+id/LinearLayout2" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 
    </LinearLayout> 

    </RelativeLayout> 

</ScrollView> 

我有這樣的:

|one two three four five s| 
|       | 
|       | 

我想這樣:

|one two three four five | 
|six seven eight ten  | 
|       | 

編輯:

如果我改變 newTextView.setSingleLine(真); 至 newTextView.setSingleLine(false); 那麼我有這樣的:

|one two three four five six | 
|       sev| 
|       en| 

回答

0

爲你設置爲true在此newTextView.setSingleLine(true);你需要的,如果你想要的文字是包裹在下一行

只是newTextView.setSingleLine(false);改變這在for循環

設置爲假根據您的代碼,您每次都在新的TextView中添加數組對象的單個元素,並且無需爲此設置此方法,您需要爲此更改佈局。如果你正在追加或者你的p字符串設置在單個文本視圖中,你可以隨心所欲地使用多個文本視圖,但是你需要改變你的佈局。

單一的TextView

TextView newTextView = new TextView(this); 
newTextView.setText(p); 
newTextView.setBackgroundColor(Color.RED); 
newTextView.setTextSize(20); 

groupLL.addView(newTextView, mlp); 
+0

謝謝你,但是這是沒有好處的。看看我的編輯了。 – user1708275

+0

檢查更新回答 – Pratik

0

設置以下爲false,而不是真正使包裝newTextView.setSingleLine(true);

+0

謝謝,但這不好。看看我的編輯了。 – user1708275

+0

您是否嘗試過在地方插入\ n,您想在哪裏換行? – slezadav

+0

不是 - 因爲我不知道我想在哪裏換行。我不知道什麼是字符串p。換行符必須位於屏幕的右端。 – user1708275

0

變化

newTextView.setSingleLine(true); 

newTextView.setSingleLine(false); 

希望它會工作,我的朋友......

+0

謝謝,但這不好。看看我的編輯了。 – user1708275

1

嘗試設置setEllipsize爲textviews

newTextView.setEllipsize(null);