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

Android 官方教你如何讓應用完美適配各種螢幕尺寸

編輯:Android 開發實例

本文將帶你了解如何讓應用支援各種不同螢幕尺寸,主要途徑有以下幾種:

 

  • 讓布局能充分自適應螢幕
  • 依螢幕配置載入合適的 UI 布局
  • 確保正確的布局被套用在正確的裝置上
  • 提供能隨螢幕大小自動伸縮的圖片

使用 "wrap_content" 與 "match_parent"

要確保布局能自適應各種螢幕,你應該在視圖的寬高上使用 "wrap_content" 與 "match_parent"。設為 "wrap_content" 時,視圖的寬高會被設成剛好能容納內容的最小值;而設為 "match_parent"(在 Android API 8 之前稱為 "fill_parent")時,視圖的寬高會延伸填滿整個父布局。

用 "wrap_content" 與 "match_parent" 取代寫死尺寸,視圖要嘛只佔用剛好需要的空間,要嘛填滿所有可用空間。例如:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:orientation="vertical" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.     <LinearLayout android:layout_width="match_parent"   
  6.                   android:id="@+id/linearLayout1"    
  7.                   android:gravity="center" 
  8.                   android:layout_height="50dp"> 
  9.         <ImageView android:id="@+id/imageView1"   
  10.                    android:layout_height="wrap_content" 
  11.                    android:layout_width="wrap_content" 
  12.                    android:src="@drawable/logo" 
  13.                    android:paddingRight="30dp" 
  14.                    android:layout_gravity="left" 
  15.                    android:layout_weight="0" /> 
  16.         <View android:layout_height="wrap_content"   
  17.               android:id="@+id/view1" 
  18.               android:layout_width="wrap_content" 
  19.               android:layout_weight="1" /> 
  20.         <Button android:id="@+id/categorybutton" 
  21.                 android:background="@drawable/button_bg" 
  22.                 android:layout_height="match_parent" 
  23.                 android:layout_weight="0" 
  24.                 android:layout_width="120dp" 
  25.                 style="@style/CategoryButtonStyle"/> 
  26.     </LinearLayout> 
  27.  
  28.     <fragment android:id="@+id/headlines"   
  29.               android:layout_height="fill_parent" 
  30.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  31.               android:layout_width="match_parent" /> 
  32. </LinearLayout> 

注意上例中如何用 "wrap_content" 與 "match_parent" 定義控制項的寬高,這讓整個布局能正確適應不同螢幕,甚至橫屏。

 

下圖是這個布局分別在直屏與橫屏下的顯示結果,可以留意控制項的寬高是隨螢幕自適應的。


 

 

使用 RelativeLayout

 

透過多層嵌套 LinearLayout 並搭配 "wrap_content" 與 "match_parent",已經能搭出相當複雜的布局。但 LinearLayout 無法讓你精確控制子視圖之間的位置關係,它的子視圖只能一個挨著一個排下去。如果你需要更靈活的排列方式,而非單純排成一列或一行,RelativeLayout 會是更好的選擇。它允許子控制項之間以相對定位的方式擺放,例如讓一個子視圖靠螢幕左側對齊、另一個靠右側對齊。

例如:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.     <TextView 
  6.         android:id="@+id/label" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="wrap_content" 
  9.         android:text="Type here:"/> 
  10.     <EditText 
  11.         android:id="@+id/entry" 
  12.         android:layout_width="match_parent" 
  13.         android:layout_height="wrap_content" 
  14.         android:layout_below="@id/label"/> 
  15.     <Button 
  16.         android:id="@+id/ok" 
  17.         android:layout_width="wrap_content" 
  18.         android:layout_height="wrap_content" 
  19.         android:layout_below="@id/entry" 
  20.         android:layout_alignParentRight="true" 
  21.         android:layout_marginLeft="10dp" 
  22.         android:text="OK" /> 
  23.     <Button 
  24.         android:layout_width="wrap_content" 
  25.         android:layout_height="wrap_content" 
  26.         android:layout_toLeftOf="@id/ok" 
  27.         android:layout_alignTop="@id/ok" 
  28.         android:text="Cancel" /> 
  29. </RelativeLayout> 

下圖展示這個布局在 QVGA 螢幕上的結果。

 


 

下圖展示它在更大螢幕上的結果。


 

可以注意到,即使螢幕尺寸改變,各視圖之間的相對位置都沒有變動。

 

使用 Size 限定符

 

雖然上述幾種方式能解決螢幕適配問題,但那些靠伸縮控制項來適應各種螢幕的布局,未必能提供最好的用戶體驗。你的應用不僅要能自適應,還應該能依螢幕配置載入不同布局,這可以透過配置限定符(configuration qualifiers)來實現。配置限定符讓程式在執行時依當前裝置配置自動載入合適的資源(例如為不同尺寸螢幕設計不同布局)。

如今很多應用為了支援大螢幕,都會實作「two pane」模式(左側面板展示含子項的清單,右側面板展示內容)。平板與電視的螢幕夠大,能同時顯示兩個面板;而手機一次只能顯示一個,兩個面板得分開呈現。因此要實作這種布局,你可能需要以下檔案:

res/layout/main.xml,single-pane(預設)布局:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:orientation="vertical" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.  
  6.     <fragment android:id="@+id/headlines" 
  7.               android:layout_height="fill_parent" 
  8.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  9.               android:layout_width="match_parent" /> 
  10. </LinearLayout> 

res/layout-large/main.xml,two-pane 布局:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="horizontal"> 
  5.     <fragment android:id="@+id/headlines" 
  6.               android:layout_height="fill_parent" 
  7.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  8.               android:layout_width="400dp" 
  9.               android:layout_marginRight="10dp"/> 
  10.     <fragment android:id="@+id/article" 
  11.               android:layout_height="fill_parent" 
  12.               android:name="com.example.android.newsreader.ArticleFragment" 
  13.               android:layout_width="fill_parent" /> 
  14. </LinearLayout> 

請注意第二個布局的目錄名含有 large 限定符,那些被判定為大螢幕的裝置(例如 7 吋以上的平板)會自動載入此布局,小螢幕裝置則載入另一個預設布局。

 

 

使用 Smallest-width 限定符

 

使用 Size 限定符有個讓不少開發者頭疼的問題:large 到底是指多大?很多應用希望能更自由地為不同螢幕載入不同布局,不管它們是否被系統判定為 "large"。這正是 Android 為什麼在 3.2 之後引入了 "Smallest-width" 限定符。

Smallest-width 限定符允許你設定一個具體的最小值(以 dp 為單位)來描述螢幕。例如 7 吋平板的最小寬度是 600dp,所以若你想讓 UI 在這種螢幕上顯示 two pane、在更小螢幕上顯示 single pane,就可以用 sw600dp 表示想在 600dp 以上寬度的螢幕使用 two pane 模式。

res/layout/main.xml,single-pane(預設)布局:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:orientation="vertical" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.  
  6.     <fragment android:id="@+id/headlines" 
  7.               android:layout_height="fill_parent" 
  8.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  9.               android:layout_width="match_parent" /> 
  10. </LinearLayout> 

res/layout-sw600dp/main.xml,two-pane 布局:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="horizontal"> 
  5.     <fragment android:id="@+id/headlines" 
  6.               android:layout_height="fill_parent" 
  7.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  8.               android:layout_width="400dp" 
  9.               android:layout_marginRight="10dp"/> 
  10.     <fragment android:id="@+id/article" 
  11.               android:layout_height="fill_parent" 
  12.               android:name="com.example.android.newsreader.ArticleFragment" 
  13.               android:layout_width="fill_parent" /> 
  14. </LinearLayout> 

這意味著,最小螢幕寬度大於 600dp 的裝置會選 layout-sw600dp/main.xml(two-pane),更小螢幕的裝置則選 layout/main.xml(single-pane)。

 

然而,早於 Android 3.2 的系統無法識別 sw600dp 這個限定符,所以你仍需同時使用 large 限定符,也就是要在 res/layout-large 與 res/layout-sw600dp 兩個目錄下各放一份相同的 main.xml。下一節你會看到如何避免這種重複定義。

 

使用布局別名

 

Smallest-width 限定符僅在 Android 3.2 及之後的系統有效,因此你也需要同時用 Size 限定符(small、normal、large、xlarge)來兼容更早的系統。例如你想在手機上顯示 single-pane、在 7 吋平板及更大螢幕上顯示 multi-pane,就需要提供以下檔案:

  • res/layout/main.xml:single-pane 布局
  • res/layout-large:multi-pane 布局
  • res/layout-sw600dp:multi-pane 布局

後兩個檔案內容完全相同,為了解決這種重複,你可以使用別名技巧。例如定義以下布局:

 

  • res/layout/main.xml,single-pane 布局
  • res/layout/main_twopanes.xml,two-pane 布局

 

再加入以下兩個檔案:

res/values-large/layout.xml:

  1. <resources> 
  2.     <item name="main" type="layout">@layout/main_twopanes</item> 
  3. </resources> 

 

res/values-sw600dp/layout.xml:
 

  1. <resources> 
  2.     <item name="main" type="layout">@layout/main_twopanes</item> 
  3. </resources> 

這兩個檔案內容一致,但它們並沒有真正定義布局,只是替 main 定義了一個別名 main_twopanes。兩份 layout.xml 都只引用 @layout/main_twopanes,就避免了重複定義布局檔。

 

使用 Orientation 限定符

 

 有些布局在橫屏與直屏下都顯示得不錯,但多數情況下仍有調整空間。在 News Reader 範例程式中,布局在不同螢幕尺寸與方向下的顯示是這樣的:

 

  • 小螢幕,直屏:單面板,顯示 logo
  • 小螢幕,橫屏:單面板,顯示 logo
  • 7 吋平板,直屏:單面板,顯示 action bar
  • 7 吋平板,橫屏:雙面板,寬,顯示 action bar
  • 10 吋平板,直屏:雙面板,窄,顯示 action bar
  • 10 吋平板,橫屏:雙面板,寬,顯示 action bar
  • 電視,橫屏:雙面板,寬,顯示 action bar

這些布局都定義在 res/layout/ 目錄下,為了讓裝置依螢幕配置載入正確布局,程式需要使用布局別名來實現。

res/layout/onepane.xml:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:orientation="vertical" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.  
  6.     <fragment android:id="@+id/headlines" 
  7.               android:layout_height="fill_parent" 
  8.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  9.               android:layout_width="match_parent" /> 
  10. </LinearLayout> 

res/layout/onepane_with_bar.xml:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:orientation="vertical" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"> 
  5.     <LinearLayout android:layout_width="match_parent"   
  6.                   android:id="@+id/linearLayout1"    
  7.                   android:gravity="center" 
  8.                   android:layout_height="50dp"> 
  9.         <ImageView android:id="@+id/imageView1"   
  10.                    android:layout_height="wrap_content" 
  11.                    android:layout_width="wrap_content" 
  12.                    android:src="@drawable/logo" 
  13.                    android:paddingRight="30dp" 
  14.                    android:layout_gravity="left" 
  15.                    android:layout_weight="0" /> 
  16.         <View android:layout_height="wrap_content"   
  17.               android:id="@+id/view1" 
  18.               android:layout_width="wrap_content" 
  19.               android:layout_weight="1" /> 
  20.         <Button android:id="@+id/categorybutton" 
  21.                 android:background="@drawable/button_bg" 
  22.                 android:layout_height="match_parent" 
  23.                 android:layout_weight="0" 
  24.                 android:layout_width="120dp" 
  25.                 style="@style/CategoryButtonStyle"/> 
  26.     </LinearLayout> 
  27.  
  28.     <fragment android:id="@+id/headlines"   
  29.               android:layout_height="fill_parent" 
  30.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  31.               android:layout_width="match_parent" /> 
  32. </LinearLayout> 

res/layout/twopanes.xml:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="horizontal"> 
  5.     <fragment android:id="@+id/headlines" 
  6.               android:layout_height="fill_parent" 
  7.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  8.               android:layout_width="400dp" 
  9.               android:layout_marginRight="10dp"/> 
  10.     <fragment android:id="@+id/article" 
  11.               android:layout_height="fill_parent" 
  12.               android:name="com.example.android.newsreader.ArticleFragment" 
  13.               android:layout_width="fill_parent" /> 
  14. </LinearLayout> 

res/layout/twopanes_narrow.xml:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="horizontal"> 
  5.     <fragment android:id="@+id/headlines" 
  6.               android:layout_height="fill_parent" 
  7.               android:name="com.example.android.newsreader.HeadlinesFragment" 
  8.               android:layout_width="200dp" 
  9.               android:layout_marginRight="10dp"/> 
  10.     <fragment android:id="@+id/article" 
  11.               android:layout_height="fill_parent" 
  12.               android:name="com.example.android.newsreader.ArticleFragment" 
  13.               android:layout_width="fill_parent" /> 
  14. </LinearLayout> 

現在所有需要的布局都定義好了,剩下只要用限定符讓各裝置依螢幕配置載入正確布局即可。你可以使用布局別名技術:

 

res/values/layouts.xml:

  1. <resources> 
  2.     <item name="main_layout" type="layout">@layout/onepane_with_bar</item> 
  3.     <bool name="has_two_panes">false</bool> 
  4. </resources> 

res/values-sw600dp-land/layouts.xml:

  1. <resources> 
  2.     <item name="main_layout" type="layout">@layout/twopanes</item> 
  3.     <bool name="has_two_panes">true</bool> 
  4. </resources> 

res/values-sw600dp-port/layouts.xml:

  1. <resources> 
  2.     <item name="main_layout" type="layout">@layout/onepane</item> 
  3.     <bool name="has_two_panes">false</bool> 
  4. </resources> 

res/values-large-land/layouts.xml:

  1. <resources> 
  2.     <item name="main_layout" type="layout">@layout/twopanes</item> 
  3.     <bool name="has_two_panes">true</bool> 
  4. </resources> 

res/values-large-port/layouts.xml:

  1. <resources> 
  2.     <item name="main_layout" type="layout">@layout/twopanes_narrow</item> 
  3.     <bool name="has_two_panes">true</bool> 
  4. </resources> 

 

 

使用 Nine-Patch 圖片

 

支援不同螢幕尺寸,通常也意味著你的圖片資源需要具備自適應能力。例如一顆按鈕的背景圖,必須能隨按鈕大小的變化而變化。

如果你想用普通圖片來實現,很快就會發現結果令人失望,因為執行時圖片會被均勻地拉伸或壓縮。解決方案是使用 nine-patch 圖片——這是一種經過特殊處理的 PNG 圖片,你能指定哪些區域可拉伸、哪些不可。

因此,當你設計需要在不同大小控制項中使用的圖片時,最好的方式就是用 nine-patch 圖片。要將圖片轉換成 nine-patch,可以從一張普通圖片開始:


 

然後用 SDK 附帶的 draw9patch 工具(位於 SDK 的 tools 目錄)打開它,在圖片的左邊框與上邊框上繪製,標記哪些區域可被拉伸;也可以在右邊框與下邊框上繪製,標記內容要放置的區域。結果如下圖:


 

注意圖片邊框上的黑色像素:上邊框與左邊框的部分代表圖片需要拉伸時就拉伸黑點標記的位置;下邊框與右邊框的部分代表內容將被放置的區域。

同時要留意,這張圖片的副檔名是 .9.png。你必須使用這個副檔名,因為系統正是靠它來區分 nine-patch 圖片與普通 PNG 圖片。

當你在控制項中使用 nine-patch 圖片時(如 android:background="@drawable/button"),系統會依控制項大小自動拉伸你想拉伸的部分,效果如下圖:


 

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