編輯:Android 開發實例
App 的下一個使用者,可能住在米蘭、大阪或首爾。要讓他們覺得這個 App 是為自己做的,最基本的一步就是讓介面說他們的語言。Android 的資源系統把這件事變得相當直覺:你只要按語言準備好字串檔案,剩下的交給系統判斷。
在 res 底下建立 values- 加上語言代碼的資料夾,Android 就會在該語言環境下優先採用裡面的資源。
以義大利文為例,資料夾名稱是 values-it:
接著把預設的 values/strings.xml 複製到新資料夾,只修改值。切記:name 屬性要與預設檔案完全相同,這是系統配對的依據。
<?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,SDK 選用較新版本。 |
| 2 | 修改 src/MainActivity.java,加入必要程式碼。 |
| 3 | 修改 res/layout/activity_main.xml,放入 TextView。 |
| 4 | 修改 res/values/string.xml,加入英文預設字串。 |
| 5 | 新增 res/values-it/string.xml,加入義大利文翻譯。 |
| 6 | 執行 App,切換裝置語言後驗證結果。 |
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;
}
}
<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/textV8iew1"
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>
<?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>
<?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>
<?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 安裝到模擬器並啟動,此時裝置語言為英文,畫面顯示預設字串:
接著從系統設定把語言改為義大利文(設定 → 語言與輸入 → 語言)。
再次開啟 App,同一行文字已換成義大利文:
同樣的資料夾命名規則也適用於圖片、版面配置、陣列,甚至動畫。差異只在資源類型前綴,例如 drawable-it、layout-it。此外,若某語言的翻譯缺少某個 key,系統會自動回退到 values/strings.xml——因此預設檔案務必保持完整,它是所有語言的最後依靠。至於日期、貨幣與數字格式,則建議改用 DateFormat、NumberFormat 等 API 來輸出,不要自己組字串。
封裝 SQLite 分頁表格控件 GVTable:表格區與分頁列的完整實作
前一篇談了 SQLite 的分頁讀取,但那個版本只是把資料印在文字方塊裡。這次往前一步:把「分頁 + 表格呈現」封裝成一個可重複使用的自訂控件。 執行結果: 為什
Android 載入動畫:Spinner 元件實作教學
當 App 在背後忙著抓資料、算報表時,畫面上不能一片死寂。使用者需要一個訊號,知道自己按下的動作正在被處理——這就是進度條的用途。 Android 的進度條分成兩類:能
Android 介面設計趨勢:左側隱藏菜單、右側顯示內容
相關文章:android popwindow 實現左側彈出菜單層https://www.jb51.net/article/33533.htm移動 App 設計的 13 大
Android 顏色選擇器開發:SeekBar 重繪問題排查
Android 沒有內建完整的顏色選擇器,因此許多應用會自行實作。常見做法是上方顯示主色條,下方顯示明暗或飽和度調整,並以 SeekBar 或自訂控制項處理拖動。色彩條實