編輯:Android 開發實例
一支上架 Google Play 的 App,面對的是全世界的使用者。要讓它在不同地區都讀起來順暢,就必須把介面文字、數字格式與其他資源依語言環境做調整。Android 的資源系統天生就是為這件事設計的。
本章說明最基礎也最關鍵的一塊:字串的多語系化。學會這套規則之後,圖片、版面配置、配色都能用同一個邏輯替換。
核心概念只有一句話:在 res 底下建立 values- 加上語言代碼的資料夾,Android 就會依裝置語言自動挑選對應的資源。
例如要支援義大利文,就建立 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>
其他語言的代碼對照如下:
| 語言 | 代碼 | 資料夾名稱 |
|---|---|---|
| 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 |
這個範例會依裝置語言顯示英文或義大利文的問候語。
| 步驟 | 說明 |
|---|---|
| 1 | 用 Android Studio 建立專案並命名為 Locals,Target SDK 選擇較新版本 |
| 2 | 修改 src/MainActivity.java,加入必要程式碼 |
| 3 | 修改 res/layout/activity_main.xml,加入對應元件 |
| 4 | 修改 res/values/string.xml,補上預設字串 |
| 5 | 新增 res/values-it/string.xml,補上義大利文字串 |
| 6 | 執行 App,選擇真機或模擬器安裝並驗證結果 |
主 Activity 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/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:
這時候問候語就自動換成了 Ciao mondo!。實務上有幾個眉角值得注意:字串若要帶有格式(如 %1$s 佔位符),各語言版本都必須保留相同的佔位符數量與型別,否則會在執行期出錯;另外,德文、俄文等語言的字通常比英文長,版面設計時要預留擴展空間,避免文字被截斷。
Android JSON 解析入門:用 JSONObject 取出你真正需要的那幾個欄位
JSON(JavaScript Object Notation)是一種不依附任何程式語言的純文字資料格式。它不像 XML 需要層層疊疊的標籤與屬性宣告,因此在行動網路上傳
Android JSON 解析實戰:用 JSONObject 取出你要的欄位
JSON(JavaScript Object Notation,JavaScript 物件符號法)是一種以純文字描述的資料交換格式。因為語法輕巧、人眼可讀,而且天生就能對
Android 自定義格式 Button 的布局思路
在 Android 中,可以完全透過 XML 自定義 Button 的外觀,而不依賴圖片素材。常見做法是使用 selector 搭配 shape,定義按壓狀態、漸層、邊框
Android 彈出視窗的兩種做法:PopupWindow 與 Activity 偽窗口
目前見過的彈出視窗實作主要有兩種:最常見的 PopupWindow,以及用 Activity 假裝成彈窗的寫法。後者看起來相當有創意——沒想到 Activity 也能拿來