如何在字符串资源中放入变量?

有没有可能把变量放在字符串资源中? 如果有,我怎么使用它们。

我需要的是:

<string name="next_x_results">Next %X results</string>

并将一个 int 代替% X。

63997 次浏览
<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>




int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage, numPoundsMeat);

取自 给你的例子

是的,你可以用。 在将标记添加到 string.xml 文件之后,然后在您的。Java 文件,你可以按照这个。

AndriodApp

String str = getResources () . getString (R.String.name) ;

只需将它作为 format Args 对象传递给 getString ()函数即可。

int nextResultsSize = getNextResultsSize();
String strNextResultsSize =
getResources().getString(R.string.next_x_results, nextResultsSize);

XML:

<string name="next_x_results">Next %1$d results</string>
<string name="message">You shot %1$d pounds of meat! Put Second Variable String here %2$s and third variable integer here %3$d</string>
int intVariable1 = 123;
String stringVariable2 = "your String";
int intVariable3 = 456;
String strMeatFormat = getResources().getString(R.string.message, intVariable1, stringVariable2 , intVaribale3);