【翻译】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.
相关文章
相关标签/搜索