然後下面是自己的理解:
String ,string-array,
Plurals
都是可以定義在 :res/values/strings.xml:
大家對String ,string-array 可能很明白,用的比較的多,因為直接alt+/
快鍵方式就有提示,然後你就能看到 這些標籤
用起來也很簡單,在/strings.xml: 定義之後,可以通過 r.strinb.xxxx 能找到你定義
<?xml version="1.0" encoding="utf-8"?><resources>
<string name="hello">Hello!</string></resources>
做了android也快很久了,就是沒有養成認真看androidSdk 文檔的習慣 ,一貫都是用到哪就去尋找,所以今天反編譯別人的代碼看到了
Plurals 這個標籤,感覺自己沒用過,直接去sdk 中尋找,發現很使用的一個標籤,其實android給了很多方便的便簽和最佳化的解決方案,關鍵是你是否認真的查看的了
他官方的文檔:
廢話補不多說: sdk 介紹:http://www.gfan.com/dev/android/guide/topics/resources/string-resource.html
xml中可以這樣引用<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> 程式中代碼可以這樣得到String string = getString(R.string.hello); 這是 string-array
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item>
</string-array></resources> 程式碼:Resources res = getResources();String[] planets = res.getStringArray(R.array.planets_array); Plurals 一對字串對應了不好幾個不同的詞語或者短語,在你的應用的程式中可以一個string 對用引用他,當你請求擷取Plurals(複數)中一個資源的時候你可以通過getQuantityString(), 但是你指定count 這個參數,這樣你就能擷取你定義好的複數對應的參數。
簡單的說就是用一個字串對應多個短語,但是你可以根據角標來確定你最後到底對應把一個,跟switch 很像<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numberOfSongsAvailable">
<item quantity="one">One song found.</item>
<item quantity="other">%d songs found.</item>
</plurals>
</resources>java 代碼int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count);
note:注意的是 quantity 只能取one ,zero ,other 枚舉是有限的 最後就是
Formatting and Styling
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and%2$d is a decimal number. You can format the string with arguements from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
這樣你應該懂了
好了希望對你有協助:轉載請註明出入!