1. 程式人生 > >在Android中使用正確的姿勢寫一個string.xml

在Android中使用正確的姿勢寫一個string.xml

首先,本文用於記錄Android中瑣碎的string.xml宣告。

不要複用
不要複用同一個string。

1.假如你在登陸和註冊介面都用了一個Dialog來提示處理中,我們需要再string.xml中宣告—R.string.loading.並把這個string應用到各自的Dialog中去。

<string name="loading">Loading please wait...</string>

版本釋出之後產品經理說這樣提示不太友好,登陸應該是“Sign in…”,註冊才應該是“Sign up…”,這時候你就要再去宣告一個string並且還需要在程式碼中做修改,所以一開始為什麼不分開宣告string呢。

    <string name="sign_in_loading">Sign in...</string>
    <string name="sign_up_loading">Sign up...</string>

2.如果你的應用國際化這時候你可能無法預知是該顯示哪種語言,不一定那個國家或者地區就能用同一個詞來顯示同一個string的意思。

    <string name="download_file_yes">Yes</string>
    <string name="terms_of_user_yes"
>Yes</string>
    <string name="download_file_yes">下載完成</string>
    <string name="terms_of_user_yes">同意條款</string>

是吧,如果是英文兩個意思相同的能用同一個詞來表示,但是在中文下就是兩個不同意思了。(ps: 原文中用的不知道是哪國的語言,我採用了中文替換)。

獨立化
給不同的介面通過增加字首使它們獨立明瞭,什麼意思呢?直接看例子。

    <!--setting screen start-->
<string name="setting_log_out">Log Out</string> <string name="setting_notification">Notification</string> <string name="setting_contact_us">Contact Us</string> <!--setting screen end-->
    <!--profile screen start-->
    <string name="profile_name">Name</string>
    <string name="profile_email">Email</string>
    <string name="profile_work">Work</string>
    <!--profile screen end-->

這麼做有下面幾點好處:
1.通過增加字首能讓你一眼就看出這些strings是在哪個介面被使用。
2.更加明瞭的string.xml讓你以後再國際化中容易一個介面一個介面翻譯過去。

當然你也可以針對不同的介面通過宣告不同的string.xml來區分strings也是一種可取的方式,比如說setting_strings.xml、profile_strings.xml。

格式化
通過Resources#getString(int id, Object… formatArgs).來格式化strings。

<string name="login_welcome_back"> - welcome back</string>

假設我們這麼宣告一個string,下面這段程式碼採用“+”來拼接字串。

String username = "Jone Doe";
String result = username + getString(R.string.login_welcome_back);

不提倡採用這種方式來拼接字串,因為在其他語言中詞語順序可能有所不同,我們應該採用Resources#getString(int id, Object… formatArgs)這種方式。

<string name="login_welcome_back">$s - welcome back</string>
<string name="login_welcome_back">歡迎 - $s 回來</string>
String username = "Jone Doe";
String result = getString(R.string.login_welcome_back, username);
//英文:Jone Doe - welcome back
//中文:歡迎 - Jone Doe 回來

這裡同上採用中文跟英文的方式來做比較,因為原文那個國家字打不出來。

複數
使用Resources#getQuantityString (int id, int quantity)來處理複數。
因為在不同語言環境下複數的表現方式不一樣,所以最好不要再程式碼中來標示:

    <string name="minute">minute</string>
    <string name="minutes">minutes</string>
String text;
int minute = Calendar.getInstance().get(Calendar.MINUTE);
if (minute == 0) {
    text = getString(R.string.minute);
} else {
    text = getString(R.string.minutes);
}

正確的方式應該採用Resources#getQuantityString (int id, int quantity)來處理:

    <plurals name="minutes">
        <item quantity="one">minute</item>
        <item quantity="other">minutes</item>
    </plurals>
int minutes = Calendar.getInstance().get(Calendar.MINUTE);
String text = getResources.getQuantityString(R.plurals.minutes, minutes);

string高亮
使用html文字來使string高亮。

如果你想改變字串中的某些string高亮處理TextView — ForegroundColorSpan並不是最好的解決方式因為高亮是採取索引但是在多語言的app上並不是安全的做法。最好的方法是在string.xml中宣告一個採用html的font color標籤來的string。

假設你有這麼一個字串“Discover and play games.”你想高亮
Discover和play兩個詞,你應該這麼做:

    <string name="html_text" formatted="false">
        <![CDATA[
          <font color = \'#28b5f5\'>Discover</font>and<font color = \'#28b5f5\'>play</font>games.
        ]]>
    </string>
TextView textView = (TextView) findViewById(R.id.textview);
text.setText(Html.fromHtml(R.string.html_text));