編輯:Android 開發實例
Android App 可能同時被台灣、日本、德國的使用者安裝使用。若介面文字硬寫在程式碼或版面檔中,就永遠只能呈現一種語言。本地化要做的,是把這些文字抽成資源,並依裝置的語言環境提供對應版本。
Android 的資源系統天生支援這件事:只要資料夾命名符合限定符規則,系統在執行階段就會自動挑選最合適的一份資源,開發者完全不需要寫判斷語言的程式碼。
命名格式是 values- 接上語言代碼。以義大利文為例,資料夾名稱為 values-it,放在 res 底下:
建立好之後,把預設 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>
其他語言的代碼對照如下:
操作步驟:
src/MainActivity.java,此範例不需額外邏輯。res/layout/activity_main.xml,放入一個文字元件。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>
安裝到 AVD 並啟動,畫面如下:
接著從系統設定把裝置語言改成義大利文。
重新開啟 App,同一行文字已經換成義大利文:
幾個實務上的建議:預設的 values 一定要放完整字串,因為它是所有語言的回退版本;同一語言若地區用詞不同,可用 values-zh-rTW 這類寫法細分;至於日期、金額、地址等格式,建議改用 Android 提供的格式化類別處理,不要自己組字串。
Android 音樂播放器:MediaPlayer 播放與跳轉
若要在 Android App 中播放音樂,MediaPlayer 是最直接的選擇。它可以播放 raw 資源中的音訊檔,也能搭配 SeekBar 做進度控制。 載入音樂檔
Android 端如何解析 JSON?從天氣 API 完整實作
JSON 的全名是 JavaScript Object Notation。雖然源自 JavaScript 的物件字面語法,它早已成為獨立、跨語言的資料交換格式,憑藉著簡潔
Android仿QQ登錄界面實現原理與完整代碼
今天我們拿騰訊 QQ 練練手,一起做一個既簡潔又漂亮的登錄界面。先看官方效果圖:程序啟動後先進入啟動頁(SplashActivity),隨後跳轉到登錄頁。導航頁這裡就不展
Android MediaPlayer 完整教學:打造具備進度條的音樂播放器
Android 提供了多種控制音訊/影片檔案與串流播放的方式,其中最具代表性的是 MediaPlayer 類別。 透過 MediaPlayer,App 得以使用系統內建的