編輯:Android 開發實例
一支上架的 App,使用者可能來自任何語言環境。要讓各地使用者都讀得懂畫面上的字,就得把文字從程式碼中抽出來,按語言分別準備。Android 對這件事有完整的資源機制支援,本章說明字串本地化的做法,其他資源的概念也一樣。
做法是在 res 底下建立一個新的資源目錄,名稱為 values-local 的形式,其中 local 換成對應的區域代碼。Android 會依裝置語言自動選用。
以義大利為例,目錄名稱就是 values-it,放在 res 之下:
目錄建立好之後,把預設資料夾裡的 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 |
以下範例示範字串本地化的實際操作,做出一個能依美國與義大利地區設定顯示不同語言的 App。
你可以在實機或模擬器上執行驗證。
| 步驟 | 要做的事 |
|---|---|
| 1 | 用 Android Studio 建立專案,命名為 Locals。Target SDK 與 Compile With 設為最新版本或較高 API 等級。 |
| 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) { // 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,問候語已換成義大利文:
Android miniTwitter 登入畫面實作:漸層背景、圓角表單與相對佈局
登入畫面是許多 App 的第一印象。本文以 miniTwitter 登入頁為例,說明如何用漸層背景、圓角容器、相對佈局與表單控制項,做出一個清爽美觀的登入介面。 畫面結構
Android 檢查與設定預設應用程式的完整解析
Android 提供了設定「預設開啟程式」的機制。本文說明如何在程式中檢查是否已有預設程式、如何引導使用者變更,以及如何清除自己的預設設定。實務上分成兩種情況:已經有預設
Android 中的 TCP 與 UDP 傳輸實作
TCP 與 UDP 是 Android 網路開發中常見的傳輸協定。TCP 面向連接、可靠且按序傳輸;UDP 無連接、低延遲,適合理會即時性較高的場景。TCP 通訊概念TC
Android 開發技巧:用 ViewStub 實現控制項的惰性載入
前面介紹過 <include> 標籤,它可以在佈局檔中引用另一個佈局檔,並覆蓋被引用佈局根節點所有以 android:layout 開頭的相關屬性。透過 &l