【翻譯】Best Practices for User interface android 適配不一樣屏幕、不一樣分辨率

 

地址:http://developer.android.com/training/multiscreen/screendensities.html#TaskProvideAltBmphtml

安卓支持不一樣的屏幕大小

安卓支持很是多種屏幕,大小不一,有3.5’4.0’,5.0’。爲了讓寫出來的app可以適配這些不一樣屏幕的大小,寫程序時應該注意如下幾點:android

 

1.使用」wrap_content」 和「match_parent」web

這裏就要求儘可能不要使用固定大小的值app

2.在複雜界面佈局時,若是能使用ReleativeLayout 就不要使用使用LinearLayout。必定要是軟件使用Layout嵌套層次低!默認到了10層嵌套時就表示性能很是有問題了。less

3.使用佈局大小限定符(Size Qualifiers)ide

對於不一樣屏幕大小的佈局文件,使用限定付來制定不一樣大小的佈局佈局

通常咱們新建一個layout在以下位置:性能

res/layout/main.xml字體

若是使用支持大屏幕就會是在以下位置:ui

res/layout-large/main.xml

對於large的範圍以下:

image

參考表:

Screen characteristic Qualifier Description
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra-large size screens.

 

  • xlarge  至少  960dp x 720dp
  • large 至少  640dp x 480dp
  • normal  至少  470dp x 320dp
  • small  至少  426dp x 320dp

4.使用最小限定符(Smallest-width Qualifier) 【僅支持android 3.2以上設備】

若是你想制定,對於某個屏幕,若是其寬度大於600px,就要使用我制定的佈局時就能夠這樣建立以下的佈局文件:

res/layout-xw600dp/main.xml

5.使用佈局別名(Layout Aliases)

如上所示,對於3.2一下的設備要適配不一樣的大小的屏幕,就須要以下3個文件:

res/layout/main.xml

res/layout-large/main.xml

res/layout-sw600dp/mian.xml

但其實res/layout-large/main.xml這個文件與res/layout-sw600dp/mian.xml文件都是同樣的,爲了不這種浪費,你能夠這樣作:

先建立2個文件 :

res/layout/main.xml

res/layout/main_twopanes.xml

再增長這2個文件:

res/values-large/layout.xml:

<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

 

res/values-sw600dp/layout.xml:
<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

 

6.使用界面方向限定符(Orientation Qualifiers)

對於同一個設備屏幕,在不一樣方向的適配上(橫向、豎向),其實也有一些工做須要作。對這種可使用方向限定符 好比有1個main.xml 對於不一樣方向能夠建立這樣的佈局 res/layout/main.xml 默認佈局 res/layout/main_horiz.xml 當設備橫向時的佈局
要適配這種狀況能夠添加以下文件:
默認狀況
:res/values/layouts.xml
<resources>
    <item name="main_layout" type="layout">@layout/main</item>
    <bool name="has_two_panes">false</bool>
</resources>

 

 
 
橫向時:
:res/values-land/layouts.xml
<resources>
    <item name="main_layout" type="layout">@layout/main_horiz</item>
    <bool name="has_two_panes">true</bool>
</resources>

 

安卓自己提供了兩種限定符
Orientation land Resources for screens in the landscape orientation (wide aspect ratio).
port Resources for screens in the portrait orientation (tall aspect ratio).
7.使用.9.png
這種圖片能夠支持擴展拉伸

支持不一樣的屏幕密度(Densities)

安卓設備除了有不一樣的屏幕物理大小,同時,對於同一物理大小的設備,又可能會是不一樣的屏幕密度大小。換句話說一樣是一塊5.0英寸的屏幕,有的可能像素分辨率是 400X800,有的多是1024X1920,或者其餘

對於這種狀況google要求咱們要這樣作:

1.使用與像素無關的像素單位

之前估計你們都喜歡使用px做爲單位,如今提供了dp那麼你能夠這樣認爲,px已經再也不支持了,只使用dp。對於字體就須要使用sp

2.使用能夠替換的圖片資源文件

對於不一樣的像素密度,咱們應該提供不一樣的大小的圖片資源,這樣在適配多個界面時,纔不至於同一個佈局文件,在不一樣的像素密度的設備下,看起來差距很大。

對於一張在PC上圖片大小爲 100X100像素的image.png,爲了讓其可以適配不一樣屏幕的大小就應該對不一樣的密度建立不一樣的大小的圖片

若是咱們將其放在/res/darwable/image.png那麼其實際大小在不一樣的分辨率下就會不一樣

  • 密度因子xhdpi: 2.0    須要建立一張在PC上大小爲200X200的圖片,並將其放入到  /res/drawable-xhdpi/image.png
  • 密度因子hdpi: 1.5      須要建立一張在PC上大小爲150X150的圖片,並將其放入到  /res/drawable-hdpi/image.png
  • 密度因子mdpi: 1.0 (baseline)   就是默認的
  • 密度因子ldpi: 0.75    須要建立一張在PC上大小爲75X75的圖片,並將其放入到  /res/drawable-ldpi/image.png

其計算公式就是在PC上,分別對寬高乘以那個密度因子

google將屏幕的像素密度分爲以下幾種:

image

參考表:

Screen characteristic Qualifier Description
Density ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra-high-density (xhdpi) screens (~320dpi).
xxhdpi Resources for extra-extra-high-density (xxhdpi) screens (~480dpi).
xxxhdpi Resources for extra-extra-extra-high-density (xxxhdpi) uses (~640dpi). Use this for the launcher icon only, see note above.
nodpi Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.
tvdpi Resources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.
相關文章
相關標籤/搜索