[英]How to use integer resources within string resources in android
如何在我的 strings.xml 文件中使用我的 integers.xml 文件中存儲的一些整數。
例如:
我有<integer name="some_integer">5</integer>
我想在我的 strings.xml 文件中使用它:
<string name="some_string">This is my string num @integers/some_integer in a row</string>
顯然我的方法不夠好,所以我需要一點幫助。 我相信有一種可能的解決方案,我只是不知道正確的解決方案。
感謝所有的幫助!
簡短的版本是你不能像這樣混合資源,但你可以在 Java 中使用:
getResources.getString(R.string.some_string,
getResources.getInteger(R.integer.some_integer)
);
然后在你的字符串 XML
<string name="some_string">This is my string num %d in a row</string>
這樣%d
將被您在getString
中傳遞的整數替換
其他人說你的方法不起作用。 這是面對類似問題的舊答案:
<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>
int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage);
String strMeatMsg = String.format(strMeatFormat, numPoundsMeat);
請參閱此參考資料:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.