編輯:Android 開發實例
一個上架到 Google Play 的 App,可能同時被德國、日本、巴西的使用者安裝。如果畫面上的字永遠只有英文,對不懂英文的使用者來說,這個 App 等於不存在。本地化(Localization)就是解決這個問題的標準做法,而 Android 的資源系統天生就為它設計好了。
Android 在挑選資源時會參考裝置的語言環境(Locale)。你只要在 res 底下建立名稱為 values- 加上語言代碼的資料夾,系統就會自動比對並採用對應的那一份。
舉例來說,要支援義大利文,就建立 res/values-it。專案結構看起來像這樣:
資料夾建好之後,把預設 values/strings.xml 複製進去,然後只改「值」,不要改 name。Android 正是靠 name 把不同語言的同一句話配對起來的。
<?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>
| 語言 | 代碼 | 資料夾名稱 |
|---|---|---|
| Afrikaans | af | values-af |
| Arabic | ar | values-ar |
| Bengali | bn | values-bn |
| Czech | cs | values-cs |
| Chinese | zh | values-zh |
| German | de | values-de |
| French | fr | values-fr |
| Japanese | ja | values-ja |
需要區分同一語言的不同地區時,可以加上地區限定,例如 values-zh-rTW(台灣繁中)與 values-zh-rCN(大陸簡中)。
做一個只顯示一行字的 App,同時準備義大利文翻譯,然後透過切換裝置語言來觀察效果。
| 步驟 | 做法 |
|---|---|
| 1 | 以 Android Studio 建立專案 Locals,SDK 版本選新一些。 |
| 2 | 修改 src/MainActivity.java,補上必要程式碼。 |
| 3 | 修改 res/layout/activity_main.xml,放入 TextView。 |
| 4 | 修改 res/values/string.xml,加入預設英文字串。 |
| 5 | 新增 res/values-it/string.xml,加入義大利文翻譯。 |
| 6 | 執行 App,切換裝置語言後驗證。 |
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;
}
}
<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>
<?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>
<?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>
<?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>
把 App 裝到模擬器並啟動,預設顯示英文:
接著到系統設定把語言改成義大利文(設定 → 語言與輸入 → 語言 → Italiano)。
重新開啟 App,同一行文字已換成義大利文:
第一,預設的 values/strings.xml 一定要完整。當某語言缺少某個 key 時,系統會回退到預設值;若預設也缺,就會在執行時炸掉。第二,德文、俄文等語言的句子通常比英文長,版面配置要預留擴展空間,避免文字被截斷。第三,阿拉伯文、希伯來文屬於從右到左的書寫系統,若要在這些市場上線,版面設計一開始就該考慮 RTL 支援。
為 Android 系統加入「按電源鍵掛斷電話」功能
現在多數 Android 手機都內建了「按電源鍵結束通話」的選項,通常放在通話設定裡。但在原生原始碼中,這一項並不存在,得自己加。以下記錄我在學習過程中改動原生碼的完整思
仿 QQ 空間底部中央加號選單:Android 完整實作解析
網路上搜得到的「仿 QQ 空間底部選單」範例不少,但仔細看會發現很多問題:布局裡塞滿了寫死的魔法數字、不同解析度下位置就歪掉、PopupWindow 的生命週期也沒處理好
Android MediaPlayer 教學:打造可播放、暫停、快轉的音樂播放器
Android 提供了多種播放音訊與影片的方式,其中最直接、也最常被拿來播放應用程式內建媒體檔案的,就是 MediaPlayer 這個類別。它其實是內建媒體播放服務的一層
Android 加載 Spinner:用 ProgressBar 顯示不確定進度
在 Android 中,可以透過加載進度條來顯示任務的進展。進度條有兩種形狀:加載欄與加載微調(spinner)。本章我們將討論 spinner。 Spinner 用於顯