編輯: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
Android記事本範例剖析(二):選項、上下文與動態選單
本篇繼續以記事本範例說明 Android 的選單系統。Android 主要提供三種選單:選項選單、上下文選單與子選單。理解它們的呼叫時機,是做出順手操作介面的基礎。 An
用 Handler 實現 ViewPager 廣告頁自動輪播的完整方案
很多電商網站和 App 都有自動輪播的商品推廣圖,看起來體驗很不錯。正好今天在學習 ViewPager,於是就動手做了一個功能類似的 Demo。 下面是其中兩個效果的截圖
Android 多語系支援:strings.xml 與 values-it 目錄
Android 會依裝置語言選擇對應資源。只要建立正確的 values 目錄,就能讓同一套應用顯示不同語言。 建立語系資料夾 在 res 下新增 values-it、va
Android 音訊播放實作:MediaPlayer 與播放控制
MediaPlayer 可用來播放 raw 或指定來源的音訊。建立物件後,即可控制播放、暫停與跳轉。 MediaPlayer mediaPlayer = MediaPla