編輯:Android 開發實例
Android 應用程式可能在許多不同地區的許多裝置上執行。為了讓應用程式更具互動性、也更貼近使用者,App 應該能處理符合當地語言環境的文字、數字與檔案格式。本章要解釋的是:如何依不同區域將應用程式中使用的字串本地化,而其他內容也能以同樣的方式處理。
要為應用程式中使用的字串做本地化,需要在 res 底下建立一個新的資料夾,命名為 values- 加上地區/語言碼。例如義大利的情況,就在 res 下建立 values-it 資料夾。這顯示在下圖:
該資料夾建立之後,把預設資料夾中的 strings.xml 複製到新建的資料夾,然後更改其內容。舉例來說,這裡已經改變了 hello world 字串的值。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Ciao mondo!</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Hola Mundo!</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Bonjour le monde !</string>
</resources>
除了這些語言,其他語言的區域碼已在下面列出:
這裡示範如何使用字串的本地化。它建立一個基本的應用程式,允許依美國與義大利地區顯示不同的內容。可在實機或模擬器上試驗。
src/MainActivity.java 加入必要程式碼。res/layout/activity_main.xml 加入對應的 XML 元件。res/values/string.xml 加入必要字串。res/values-it/string.xml 並加入必要字串。主活動 src/com.yiibai.locals/MainActivity.java:
package com.example.locals;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
版面 res/layout/activity_main.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView android:id="@+id/textV8iew1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="174dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
預設字串 res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Locals</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
義大利文 res/values-it/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Locals</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Ciao mondo!</string>
</resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.yiibai.locals"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.yiibai.locals.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
試著執行修改後的本地化應用。把程式安裝到 AVD 並啟動它,若一切設定與程式都沒有問題,會顯示這樣的模擬器視窗:
現在從「選單/系統設定/語言」把裝置語言改為義大利文。
現在再次開啟應用程式,這時候會看到 Hello World 以義大利語言顯示。下圖說明了這個結果:
第一,預設值一定要齊全。任何一個 values-xx 裡缺少的鍵,都會回退到 values/strings.xml;若連預設都沒有,App 在使用該語言的裝置上就會直接崩潰。
第二,不要用字串拼接組句子。中文可以說「已下載 3 個檔案」,英文卻要說 "3 files downloaded",語序完全不同。請改用 String.format() 搭配佔位符寫在資源檔裡,例如 <string name="downloaded">已下載 %1$d 個檔案</string>,並用 <plurals> 處理單複數。
第三,別忘了 RTL 與版面擴張。德文和芬蘭文的字通常比英文長三成以上,硬性設定的 android:layout_width="200dp" 很可能把字裁掉;阿拉伯語與希伯來語則需要 android:supportsRtl="true" 並改用 start/end 取代 left/right 對齊屬性。
Android 源碼中的單例模式:定義、優點與實際案例
定義 Ensure a class has only one instance, and provide a global point of access to it.
Android JSON 解析完整指南:從 JSONObject 到天氣查詢 App 實作
JSON 的全名是 JavaScript Object Notation(JavaScript 物件符號法),它最大的優點是輕量、易讀,而且天生就適合在瀏覽器與伺服器之間
Android 登入功能實作全解析:從 textPassword 到三次失敗鎖定
登入畫面是大多數 App 的第一道門。它看似只是「兩個輸入框加一顆按鈕」,但真正做過就會發現,密碼欄的遮蔽設定、按鈕的事件綁定、錯誤態度的回饋、以及防止暴力嘗試的機制,每
Android PreferenceActivity 詳解:用系統設定頁的架構做自己的設定介面
很多書對 PreferenceActivity 幾乎一筆帶過,但它在真實專案裡非常好用。本文把它的資料保存機制、建立方式、可用元素與事件回呼一次講完。 先看長相 左圖