android 屏幕适配

来源:互联网 发布:台州网络平台赌博 编辑:程序博客网 时间:2024/05/21 21:40

Jan 4, 2015 • CoderSimple 原文传送阵

android 设备的尺寸多种多样,在适配上给开发者带来了不少的麻烦,为了让我们的程序能在不同的屏幕尺寸下优雅的显示,学习一些适配的知识变得尤为重要,这里为了加深印象,在学习中做下总结(写到后面感觉没什么好写的,都是官网的东西,真矛盾,最后还是当练习排版留下来了)。

  1. 属性值
    • 文字大小属性使用 sp,其他图像间距使用 dp,避免使用 px
    • 布局尽量使用 wrap_contentmatch_parent 或者 (fill_parent)
  2. 布局
    • 杜绝使用 AbsoluteLayout 布局(除非特别需要,但是基本不需要)
  3. 布局限定符
    使用方法:layout-尺寸限定符/最小尺寸限定符/main.xml
    • 尺寸限定符(API13 后不推荐使用)
    • 最小尺寸限定符(API13 后推荐使用)
    • 布局别名 由于最小尺寸限定符在 Android 3.2 及之前平台上无法识别,有时候在使用最小尺寸限定符布局时为了兼容,有必要再创建一个尺寸限定符的布局,我们可以在尺寸限定符的布局文件夹下在放置一份最小尺寸限定符的布局,然而这样是我们不愿意做的,这样会造成内存空间的浪费,因此在里只创建一份文件布局,通过布局的引用达到我们想要的效果,以下为官网示例。
      • res/layout/main_twopanes.xml
      • 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>
  4. 方向限定符
    屏幕的方向分为水平方向 (land) 和竖直方向 (port),使用方法同布局限定符
  5. 布局限定符 与 方向限定符 的混合使用
    layout-尺寸限定符/最小尺寸限定符-方向限定符/main.xml
  6. 图片限定符

总而言之,屏幕的适配也就是布局资源的适配,由于涉及基础知识较多,造成难度的提升,这里给出官网屏幕适配教程,所有涉及的知识基本上都有了,只要耐心的看完,相信你会有所收获。


基础知识

  1. screen size
    • small small screens are at least 426dp x 320dp
    • normal normal screens are at least 470dp x 320dp
    • large large screens are at least 640dp x 480dp
    • xlarge xlarge screens are at least 960dp x 720dp
  2. screen desity(3:4:6:8:12:16)
    • ldpi(low) ~ 120dpi
    • mdpi(medium) ~ 160dpi(base line)
    • hdpi(high) ~ 240dpi
    • xhdpi(extra-high) ~ 320dpi
    • xxhdpi(extra-extra-high) ~ 480dpi
    • xxxhdpi(extra-extra-extra-high) ~ 640dpi

      Note: the drawable-xxxhdpi qualifier is necessary only to provide a launcher icon that can appear larger than usual on an xxhdpi device. You do not need to provide xxxhdpi assets for all your app's images

      Some devices scale-up the launcher icon by as much as 25%. For example, if your highest density launcher icon image is already extra-extra-high-density, the scaling process will make it appear less crisp. So you should provide a higher density launcher icon in the drawable-xxxhdpi directory, which the system uses instead of scaling up a smaller version of the icon.

  3. orientation
    • land(landscape)
    • port(portrait)
  4. Resolution
    • The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.
  5. Density-independent pixel (dp or dip)
    • A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
    • The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen
    • px = dp * (dpi / 160)

screens ranges:
screens-ranges

0 0
原创粉丝点击