• 【翻译】Best Practices for User interface android 适配不同屏幕、不同分辨率


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

    安卓支持不同的屏幕大小

    安卓支持非常多种屏幕,大小不一,有3.5’4.0’,5.0’。为了让写出来的app能够适配这些不同屏幕的大小,写程序时应该注意以下几点:

    1.使用”wrap_content” 和“match_parent”

    这里就要求尽量不要使用固定大小的值

    2.在复杂界面布局时,如果能使用ReleativeLayout 就不要使用使用LinearLayout。一定要是软件使用Layout嵌套层次低!默认到了10层嵌套时就表示性能非常有问题了。

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

    对于不同屏幕大小的布局文件,使用限定付来制定不同大小的布局

    一般我们新建一个layout在如下位置:

    res/layout/main.xml

    如果使用支持大屏幕就会是在如下位置:

    res/layout-large/main.xml

    对于large的范围如下:

    image

    参考表:

    Screen characteristicQualifierDescription
    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 characteristicQualifierDescription
    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.
  • 相关阅读:
    iOS- 网络访问JSON数据类型与XML数据类型的实现思路及它们之间的区别
    iOS- AVSpeechSynthesizer——iOS7语音合成器
    iOS- 利用AFNetworking(AFN)
    iOS- 利用AFNetworking(AFN)
    iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
    iOS- 多线程技术的概述及优点
    150. Best Time to Buy and Sell Stock II【medium】
    213. String Compression【easy】
    495. Implement Stack【easy】
    547. Intersection of Two Arrays【easy】
  • 原文地址:https://www.cnblogs.com/pavkoo/p/4137409.html
Copyright © 2020-2023  润新知