編輯:Android 開發實例
一支 Android App 可能被安裝在世界各地、面對使用不同語言的使用者。如果介面文字全部寫死在程式碼裡,就無法適配這些環境。所謂本地化(Localization),就是讓 App 依據裝置的語言與地區設定,自動呈現合適的文字、數字格式與資源。
好消息是,Android 已經把這件事的機制內建好了。只要把可變的字串抽到資源檔,再依語言準備對應版本,系統就會在執行時自動挑選正確的那一份。
在 res 目錄下建立一個新的資源資料夾,命名規則是 values- 加上語言代碼。例如要支援義大利文,資料夾名稱就是 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>
其他常見語言的代碼與對應資料夾如下:
若同一語言在不同地區用字有差異,還可以加上地區限定符,例如 values-zh-rTW(繁體中文、台灣)與 values-zh-rCN(簡體中文、中國大陸)。
這個範例展示同一段文字在英文與義大利文環境下會有不同結果。
操作步驟:
src/MainActivity.java(此範例邏輯極簡,幾乎不需要改動)。res/layout/activity_main.xml,放進一個 TextView。res/values/string.xml,放入英文版字串。res/values-it/string.xml,放入義大利文版字串。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;
}
}
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 裝到模擬器啟動,畫面會像這樣:
接著從系統設定把裝置語言改成義大利文(設定 → 語言與輸入 → 新增語言)。
再開一次 App,這次顯示出來的就会是義大利文的問候語:
補充一點:如果某個語言沒有提供對應的 values-xx,Android 會自動回退到預設的 values。因此預設資料夾務必保持完整,這也是避免使用者看到空白或亂碼的關鍵。
Android Spinner 載入動畫:按鈕觸發與顯示控制
Spinner 是 Android 中常見的等待提示元件。當使用者觸發某個操作,但系統需要一點時間處理時,可以用轉圈動畫告知目前仍在進行中。 基本設定 在版面配置中加入
Android 自定義滑動式抽屜菜單的完整實現
Android 雖然自帶了 SlidingDrawer 和 DrawerLayout 這類抽屜效果控制項,但實際項目中,這些內建控制項往往會限制功能的發揮,難以精準滿足設
自製可滑動頁卡:用自訂 RadioButton 搭配 ViewPager 打造 TabHost 效果
實務上很多需求都不是系統內建控制項能直接滿足的,TabHost 就是一例——它只能做出比較陽春的切換效果。這時候就得靠自訂控制項來補足,本篇示範如何用自訂 RadioBu
Android 補間動畫入門:縮放、旋轉與淡入淡出一次學會
Android 上的動畫方案不少,其中最簡單也最廣泛使用的莫過於補間動畫(Tween Animation)。你只需要給定起始值、結束值、尺寸、時間長度、旋轉角度等參數,系