編輯:Android 開發實例
一支 App 上線後,可能同時被十幾個國家的使用者開啟。若畫面上的文字全是硬編碼的英文,對非英語使用者而言體驗打了折扣。Android 從架構層面就支援資源切換:你只要按語言分別準備好文字檔,系統會依裝置設定自動挑選。本章說明這個機制的實作細節。
規則很簡單:在 res 底下新增一個名為 values-<語言代碼> 的目錄,把該語言的 strings.xml 放進去。以義大利文為例,目錄名稱就是 values-it:
目錄建立好之後,把預設 values 中的 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 請設為最新版本。 |
| 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 滑動解鎖九宮格完整實作
本文概述:滑動解鎖九宮格的分析:1、需要自訂控制項; 2、需要改寫事件 onTouchEvent(); 3、需要為九個點設定序號與座標,這裡用 Map 類即可; 4、需要
Android 音訊播放全攻略:MediaPlayer 方法與實作
要在 App 裡放音樂或播影片,MediaPlayer 是最經典的選擇。它位於 android.media 套件,能播放 SD 卡裡的檔案、res/raw 中的資源,也能
Android 應用本地化:讓 App 說出使用者的語言
一支上架的 App,可能同時被義大利米蘭的上班族、日本東京的學生、德國柏林的工程師打開。使用者對文字、數字、日期與檔案格式的期待各不相同,因此「本地化」不是加分題,而是基
高仿網易新聞頂部滑動標籤列的實作
這是網易新聞的主頁。底部用 TabHost 就能做出來,很基本;真正好看的是頂部那條會滑動的標籤列——點擊任一標籤,游標方塊會平滑滑到對應位置,同時切換下方內容。第一次