2010-11-17 30 views
6

我有一個xml一些字符串,例如多個字符串添加到一個TextView

<string name="String1">My String 1</string> 
<string name="String2">My string 2</string> 

,我想在活動像我的字符串1顯示:我的字符串2

是否可以添加到同一個TextView的多

<TextView android:text="@string/String1"/> 
<TextView android:text=": "/> 
<TextView android:text="@string/String2"/> 

這樣做的問題是,如果你插入他們TableLayout內,他們被認爲是一個s的單元格,並且「:」符號不會寫在String1的旁邊(它寫在兩個字符串的中間)。

是否有可能在xml代碼中僅僅在一個TextView中加入字符串(不用Java語言編程)? 我的意思是沒有任何語法追加串像

<TextView android:text="@string/String1+:[email protected]/String2"/> 

感謝

回答

9

你不能直接在XML做到這一點,這是最好的,你可以這樣做:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string> 


Resources res = getResources(); 
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount); 
0

看到這個related question我剛纔問。

不可能對XML中的字符串做任何事情。你需要用Java來做到這一點。

1

我不是100%,但我不認爲它有可能每個TextView有超過一個android:text。

如果你將有三個TextViews,你需要像添加一些String2的(或任何視圖具有「」:

android:layout_toRightOf="@id/string1" 
0

看那API爲TableRow

android:layout_span - Defin這個孩子應該跨越多少列。

所以我認爲你可以使用不便,如:

<TableRow android:layout_span="3"> 
    <TextView android:id="@+id/your_entire_string" /> 
</TableRow> 

然後在你的Activity通過ID找到TextView與填充( 「我的字符串1」 + 「:」 + 「我的字符串2」)。

相關問題