轉載自: http://blog.csdn.net/wsywl/article/details/6555959
在Android項目布局中,資源以XML檔案的形式存儲在res/目錄下。為了更好的實作國際化及本地化,字元串集通常以XML檔案的形式存儲在res/values/目錄下。
1、純文字字元串
一般來說,使用純文字字元串僅僅需要res/values目錄下的一個XML檔案(通常命名為res/values/strings.xml,可以使用其它的檔案名替換strings),根元素為resources,希望編碼為資源的每個字元串都有一個string子元素。String元素包含name特性,它标示了此字元串的唯一名稱,還有一個文本元素,包含字元串的文本。
字元串的表示分以下三種情況:
a) 普通字元串(不含雙引号(”)及單引号(’))。其在XML檔案中如代碼一所示定義。
代碼一:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World!</string>
- </resources>
b) 字元串僅含單引号。其在XML檔案中如代碼二或代碼三(使用轉義字元反斜杠“/”)所示定義。
代碼二:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">"Hello' World!"</string>
- </resources>
代碼三:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello/' World!</string>
- </resources>
c) 其它情況下的字元串。其在XML檔案中如代碼三所示定義,即使用一個前置反斜杠進行轉義。代碼四給出了一個示例。
代碼四:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello/' World/"!</string>
- </resources>
2、格式字元串
Android與Java的其它實作一樣支援格式字元串。這裡字元串包含一些占位符,表示在運作時要使用可變資訊替換的資料(例如,Hello everyone, my name is %1$s)。占位符的規定簡述如下:其使用%[index]$[type]格式進行标記,index标記替換資源中第index個資源對應的位置,type則标示所要替換的資源的類型(s表示資源為字元串格式)。這裡給出一個格式字元串的例子,代碼五為檔案strings.xml中的内容,代碼六為進行字元串替換時的Java代碼,圖1則給出了最終的效果圖。
代碼五:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, my name is %1$s!</string>
- <string name="app_name">MyString</string>
- </resources>
代碼六:
[cpp] view plain copy
- TextView mytext = (TextView)findViewById(R.id.mystring);
- String myname = getString(R.string.hello);
- myname = String.format(myname, "clark");
- mytext.setText(myname);
圖1:
3、樣式字元串
Android中可以使用<b>、<i>及<u>的輕量級HTML标記對字元串進行樣式處理。有如下幾種方法進行字元串的樣式化。
a) 直接将HTML标記寫入字元串資源中,同時在布局檔案中直接引用。這裡給出一個例子,代碼七為檔案strings.xml中的内容,代碼八為布局檔案的内容,代碼九則給出了Activity中onCreate函數中的内容,圖2則給出了最終的效果圖。這種情況下,能使用的HTML标記僅為<i>、<b>及<u>三種。
代碼七:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>
- <string name="app_name">MyString</string>
- </resources>
代碼八:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/mystring"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
代碼九:
[cpp] view plain copy
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
圖2:
b) 字元串資源檔案中使用轉義HTML标記,同時使用Java代碼對字元串資源進行轉化。這裡給出一個例子,代碼十為檔案strings.xml中的内容,代碼十一為布局檔案的内容,代碼十二則給出了Activity中onCreate函數中的内容,其效果如圖2所示。
代碼十:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>
- <string name="app_name">MyString</string>
- </resources>
代碼十一:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/mystring"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- />
- </LinearLayout>
代碼十二:
[cpp] view plain copy
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView mytext = (TextView)findViewById(R.id.mystring);
- String myname = getString(R.string.hello);
- Spanned textspan = Html.fromHtml(myname);
- mytext.setText(textspan);
- }
c) 直接将HTML标記寫入字元串資源中,同時使用Java代碼對字元串資源進行轉化。這裡給出一個例子,代碼七為檔案strings.xml中的内容,代碼十一為布局檔案的内容,代碼十三則給出了Activity中onCreate函數中的内容,其效果如圖2所示。
代碼十三:
[cpp] view plain copy
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView mytext = (TextView)findViewById(R.id.mystring);
- mytext.setText(getResources().getText(R.string.hello));
- }
d) 字元串資源檔案中使用純文字字元串,同時使用Java代碼對字元串資源進行轉化。這裡給出一個例子,代碼十四為檔案strings.xml中的内容,代碼十五為布局檔案的内容(注意,界面顯示的是EditText),代碼十六則給出了Activity中onCreate函數中的内容,其效果如圖3所示。這裡在定義字元串的時候也可以将其直接定義在Java代碼中。
代碼十四:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, my name is clark!</string>
- <string name="app_name">MyString</string>
- </resources>
代碼十五:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:id="@+id/mystring"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- />
- </LinearLayout>
代碼十六:
[cpp] view plain copy
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- EditText mytext = (EditText)findViewById(R.id.mystring);
- mytext.setText(R.string.hello);
- Spannable spn = mytext.getText();
- spn.setSpan(new BackgroundColorSpan(Color.GRAY), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- mytext.setText(spn);
- }
圖3:
4、樣式字元串格式化
對樣式字元串進行格式化,有以下幾個步驟:
a) 對字元串資源中的HTML标記進行轉義。例如,<b>Hello World,</b> <i>my name is</i> <u>%1$s</u>!
b) 按一般情況檢索字元串資源。例如,getString(R.string.hello)。
c) 生成格式結果,確定轉義你替換的任何字元串值,防止它們包含尖括号或&符号。例如,String.format(getString(R.string.hello),TextUtils.htmlEncode(name))。
d) 通過Html.fromHtml()将實體轉義的HTML轉換為Spanned對象。例如,Spanned textspan = Html.fromHtml(myname)。
這裡給出一個例子,代碼十七為檔案strings.xml中的内容,代碼十一為布局檔案的内容,代碼十八則給出了Activity中onCreate函數中的内容,其效果如圖2所示。
代碼十七:
[cpp] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>%1$s</u>!</string>
- <string name="app_name">MyString</string>
- </resources>
代碼十八:
[cpp] view plain copy
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView mytext = (TextView)findViewById(R.id.mystring);
- String myname = getString(R.string.hello);
- myname = String.format(myname, "clark");
- Spanned textspan = Html.fromHtml(myname);
- mytext.setText(textspan);
- }
5、參考内容
(1)、http://book.douban.com/subject/5353163/
(2)、http://baike.baidu.com/view/5626871.html?fromTaglist