Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 資源的國際化:橫豎屏配置與多語系該怎麼放

編輯:Android 開發實例

實際開發時常會遇到一個情況:橫屏(land)與豎屏(port)的版面配置需要各自獨立設計。這時我們可以自訂兩份布局檔(檔名必須相同),系統預設會載入 layout 目錄裡的那一份。同樣地,應用也需要支援不同語言;當找不到對應語言的資源時,系統會退回載入 values 中的內容。

 

要讓版面能適應方向變化,需要加上 layout-land 與 layout-port 兩個目錄。系統在切換方向時,會依目前的橫豎狀態去讀對應目錄下的配置;若該目錄不存在,才會退回使用 layout 中的配置。

 

下面是我準備的三份布局:

layout(預設):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="fill_parent" android:id="@+id/text1"
 android:layout_height="wrap_content" android:text="@string/hello" />
 <Button android:id="@+id/btn1" android:text="堅"
 android:layout_width="fill_parent" android:layout_height="wrap_content">
 </Button>
 <Button android:id="@+id/btn2" android:text="橫"
 android:layout_width="fill_parent" android:layout_height="wrap_content">
 </Button>
</LinearLayout>

 

layout-land(橫屏):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:text="橫屏顯示" />
 <TextView android:layout_width="fill_parent" android:id="@+id/text1"
 android:layout_height="wrap_content" android:text="@string/hello" />
 <Button android:id="@+id/btn1" android:text="橫"
 android:layout_width="fill_parent" android:layout_height="wrap_content">
 </Button>
 <Button android:id="@+id/btn2" android:text="豎"
 android:layout_width="fill_parent" android:layout_height="wrap_content">
 </Button>
</LinearLayout>

 

layout-port(豎屏):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="fill_parent"
 android:layout_height="wrap_content" android:text="豎屏顯示" />
 <TextView android:layout_width="fill_parent" android:id="@+id/text1"
 android:layout_height="wrap_content" android:text="@string/hello" />
 <Button android:id="@+id/btn1" android:text="橫"
 android:layout_width="fill_parent" android:layout_height="wrap_content">
 </Button>
 <Button android:id="@+id/btn2" android:text="豎"
 android:layout_width="fill_parent" android:layout_height="wrap_content">
 </Button>
</LinearLayout>

 

接著是文字內容的國際化,需要加上這幾個目錄:

values-zh-rCN —— 簡體中文

values-zh-rTW —— 繁體中文

values-jp —— 日文

命名形式為 values-國家代碼。這些資源會依作業系統的語言設定被選用;若找不到對應的語言版本,就直接取用 values 裡的內容。

 

這裡只示範中文的設定。values 中的內容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, ShowTask!</string>
 <string name="app_name">ShowTask</string>
</resources>

 

values-zh-rCN 中的內容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">此程式用於顯示任務!</string>
 <string name="app_name">顯示任務</string>
</resources>

 

如此一來,同一組 string 名稱在不同語言環境下就會自動換成對應的文案,而版面配置也能隨螢幕方向切換。兩套機制其實是同一個思路的延伸:把「會變動的東西」從程式碼裡抽出來,交給資源系統依環境挑選。

 

參考文章:

https://www.fengfly.com/plus/view-194382-1.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved