地址: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的范围如下:

参考表:

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上大小为75X的图片,并将其放入到  /res/drawable-ldpi/image.png

其计算公式就是在PC上,分别对宽高乘以那个密度因子

google将屏幕的像素密度分为如下几种:

参考表:

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.

最新文章

  1. Java 设计模式 —— 单例模式
  2. LogStash的Filter的使用
  3. gfw列表
  4. 关于sql语句的优化问题
  5. JDBC 增删查改
  6. my ambition
  7. RHadoop计算平台搭建
  8. 1741. Communication Fiend(dp)
  9. SQL Join PK ChinaJoy
  10. 南京Uber优步司机奖励政策(1月11日~1月17日)
  11. JSP 语法/标签
  12. WPF 实现验证码功能
  13. 开源纯C#工控网关+组态软件
  14. DOM操作中,遍历动态集合的注意事项。ex: elem.children
  15. 济南清北学堂游记 Day 2.
  16. javaWeb学习之Listener监听
  17. linux常用命令简介
  18. 做二级菜单时候遇到的关于事件冒泡以及mouseover和mouseenter的不同
  19. OPC安装-配置(http://www.mabotech.com)
  20. Linux之更改Nginx映射默认根目录

热门文章

  1. 洛谷4316 绿豆蛙的归宿(DAG递推/概率dp)
  2. ArrayList深度分析:ArrayList和数组间的相互转换
  3. getTasksWithCompletionHandler的用法
  4. angularJs基础学习
  5. HashMap的容量大小增长原理(JDK1.6/1.7/1.8)
  6. 树莓派安装开源智能家居系统 Domoticz
  7. SQL case when then end根据某列数据内容在新列显示自定义内容
  8. jQuery中animate()方法以及$(&#39;body&#39;).animate({&quot;scrollTop&quot;:top})不被Firefox支持问题的解决
  9. 新磁盘创建lvm并挂载
  10. 分布式事务框架-fescar