編輯:Android 開發實例
同一支 App,會在不同國家、不同語言環境的裝置上被開啟。要讓各地使用者都覺得順手,App 就必須依語言環境調整文字、數字、日期與檔案格式。這件事在 Android 裡有現成的機制可以借力。
本章說明如何讓 App 依裝置語言自動切換字串;相同的思路也適用於圖片、版面等其他資源。
關鍵觀念是:所有顯示給使用者看的文字,都不該寫死在程式碼或版面檔裡,而應放進資源檔。接著在 res 底下新增資料夾,命名格式為 values- 加上語言代碼。
以義大利文為例,資料夾名稱就是 values-it:
資料夾建好後,把預設 values 裡的 strings.xml 複製過去,改寫內容即可。以下示範把同一句問候語寫成三種語言:
<?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 | 南非荷蘭語 | af | values-af |
| 2 | 阿拉伯語 | ar | values-ar |
| 3 | 孟加拉語 | bn | values-bn |
| 4 | 捷克語 | cs | values-cs |
| 5 | 中文 | zh | values-zh |
| 6 | 德語 | de | values-de |
| 7 | 法語 | fr | values-fr |
| 8 | 日語 | ja | values-ja |
這個範例示範如何讓同一支 App 依系統語言顯示不同文字。
| 步驟 | 做法 |
|---|---|
| 1 | 用 Android Studio 建立專案,命名為 Locals。 |
| 2 | 修改 src/MainActivity.java。 |
| 3 | 修改 res/layout/activity_main.xml,加入對應元件。 |
| 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) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
請注意這段程式碼裡完全沒有語言判斷的邏輯。切換語言是資源系統自動完成的,開發者不必多寫一行——這是 Android 本地化機制最省事的地方。
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/text iew1" 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="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="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,這次問候語已經變成義大利文的寫法:
若要更進階,還可以加上地區限定(如 values-zh-rTW)、用 plurals 處理可數量詞,或針對由右至左語言調整版面方向。
多方向抽屜效果:Panel 控件的使用與閃爍 bug 修復
想在 Android 上做出類似 Launcher 抽屜的效果,多數人第一個想到的是官方控件 SlidingDrawer。但本文的主角不是它,而是一個民間的控件集合:an
Android 登入介面:帳密驗證與鎖定機制
登入介面需要兼顧易用性與安全性。除了讓使用者輸入帳號與密碼,也應限制連續錯誤次數,避免暴力嘗試。 建立輸入欄位 帳號使用一般 EditText;密碼欄位要設定 textP
Android 示例程式剖析之 Snake 貪吃蛇(二:FrameLayout 與 RelativeLayout)
本文以 Snake 專案為例,說明 FrameLayout 與 RelativeLayout 的差異,並示範如何用 merge 標籤減少多餘的版面節點。FrameLayo
Android 開發技巧:點擊 a 標籤或 TextView 連結彈出自定義 Activity
前面介紹過 <a> 標籤以及 TextView 自動識別的特殊文字(網址、電話號、Email 等),這些都能透過單擊觸發不同動作。這些內建行為已能滿足大多數需