編輯:Android 開發實例
一支上架的 Android App,可能會被安裝在幾十個國家的裝置上。使用者期待的是打開來就看到自己熟悉的語言、符合本地習慣的數字與日期格式。這種「讓 App 融入當地環境」的工作,就稱為本地化(Localization)。
本章要說明的是最基礎也最常被用到的一塊:如何讓 App 依據裝置的語言環境,自動切換畫面上的文字。其他資源(圖片、音效、版面)也能用同一套機制處理。
原理很單純:在 res 底下建立以「values-」開頭、後面接語言代碼的資料夾,Android 在執行時就會依裝置語系自動挑選對應的那一份資源。比方說要支援義大利文,就建一個 res/values-it:
資料夾建好後,把預設 res/values 裡的 strings.xml 複製一份進去,然後只改文字的內容、保留相同的 name。以下示範同一句 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>
常見語言對應的代碼與資料夾名稱整理如下,需要時直接照著建資料夾即可:
| 編號 | 語言 | 代碼 | 資料夾名稱 |
|---|---|---|---|
| 1 | 南非荷蘭語 Afrikaans | af | values-af |
| 2 | 阿拉伯語 Arabic | ar | values-ar |
| 3 | 孟加拉語 Bengali | bn | values-bn |
| 4 | 捷克語 Czech | cs | values-cs |
| 5 | 中文 Chinese | zh | values-zh |
| 6 | 德語 German | de | values-de |
| 7 | 法語 French | fr | values-fr |
| 8 | 日語 Japanese | ja | values-ja |
如果只想針對「某國的中文」做調整,可以把語言與地區串起來,例如 values-zh-rTW 代表台灣繁體中文,values-zh-rCN 代表中國大陸簡體中文。
這個範例做了兩份字串資源(預設英文與義大利文),切換裝置語言後,同一行文字會顯示不同結果。可在實機或模擬器上驗證。
| 步驟 | 做法 |
|---|---|
| 1 | 用 Android Studio 建立專案,命名為 Locals,Target SDK 與 Compile With 對到最新版 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 並在裝置上驗證。 |
主活動 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) { // Inflate the menu; this adds items to the action bar if it is present. 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/textView1" 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/string.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/string.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>
安裝並啟動 App,畫面上會先出現預設的英文:
接著到裝置的「設定 → 語言與輸入 → 語言」,把系統語言改成義大利文:
回到 App 重新開啟,同一行文字就換成義大利文了:
幾個實務提醒:預設的 res/values 一定要放一份完整字串,這是語系不支援時的後備;字串若要插入變數,別用字串拼接,改用格式化佔位符並搭配 getString(R.string.xxx, arg),因為不同語言的語序可能不同;另外阿拉伯語、希伯來語屬右到左書寫系統,版面需要開啟 RTL 支援並另外提供對應的版面配置。
Android 下拉刷新控制項 RTPullListView 範例
RTPullListView 可用於下拉刷新與載入更多。頂部樣式可改 pulllist_head.xml,底部可改 list_footview.xml。Activity
Ubuntu 下用 adb 連接小米 2 的完整調試配置步驟
在 Ubuntu 上連接國產手機做 adb 調試,往往不會像插入即識別那樣順利。小米 2 的 USB 廠商 ID 並不在 adb 的默認列表裡,需要手動補上。整個過程可以
Android 表單字段驗證的實例代碼詳解
在登錄、註冊這類表單場景中,字段驗證是必不可少的一環——用戶名不能為空、郵箱格式要正確、手機號要合法等。借助開源的 Android-Validator 庫,我們能優雅地完
Windows 上架設 Android 開發環境完整指南(JDK、Eclipse、SDK、ADT)
本文寫給第一次想在 Windows 上打造 Android 開發環境的朋友,把整個流程拆成五個步驟,盡量把每個環節講清楚,讓你能一次把環境搭好。 五個步驟分別是: 第一步