編輯:Android 開發實例
一款 Android App 很可能會在不同國家、不同地區的裝置上被使用。為了讓應用更貼近使用者,App 應該依據裝置所在的語言環境,自動調整文字、數字、檔案等內容的呈現方式。
本章要說明如何針對不同地區進行本地化,重點放在字串的在地化處理,其他資源也能用類似手法完成。
要讓 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>
除了以上幾種語言,下表也整理了一些常見語言的代碼與對應資料夾名稱:
以下範例示範字串本地化的用法,打造一個能依美國與義大利地區自動切換顯示的簡易 App。
你可以把 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 安裝到 AVD 並啟動,若一切正常,模擬器會顯示如下畫面:
接著從「設定/系統/語言」把裝置語言切換成義大利文。
再次開啟 App,你會看到 Hello World 已變成義大利文的問候語,如下圖所示:
Android 載入轉圈 Spinner 實作教學
當 Android App 執行某項工作,但無法確定完成時間時,可以使用不確定模式的進度條,也就是常見的載入 Spinner。它通常用一個旋轉的圓圈表示「正在處理中」。
Android MediaPlayer 教學:做一個能播放、暫停與快轉的音樂播放器
Android 想播放音訊或影片,最直接的工具就是 MediaPlayer 這個類別。它被封裝在系統的媒體播放服務之上,能處理本機檔案、資源目錄中的素材,也能拉串流。
ListView 中讓 Button 始終固定在底部的範例
在 Android 中實作底部佈局,往往會使用 RelativeLayout 並設定 android:layout_alignParentBottom=true,這樣很容
Android 解析 JSON 資料:從文字到可用欄位的完整流程
JSON(JavaScript Object Notation)是一種與程式語言無關的輕量資料交換格式,語法簡單、可讀性高,早已成為 XML 之外的主流選擇。本章示範如何