android资源适配

android程序在不同的环境(如分辨率不同的机型,不同语言的操作系统)或状态(如横竖屏)下,需要不同的资源(如图片,语言),一般情况下,这些资源是自适配的,只需要命名不同的资源文件夹,把同名的资源放入这些文件夹中即可

  比如下面就是一个程序项目的资源文件夹:

 



不同的命名代表什么含义呢?详细见下表:

限定符Qualifier

Values

移动国家码MCC和移动网络码MNC

手机设备SIM卡上的移动国家码和移动网络码。比如mcc310-mnc004 (美国,Verizon品牌); mcc208-mnc00 (法国,Orange品牌); mcc234-mnc00 (英国,BT品牌). 
如果这个设备使用一个无线连接(GSM电话),则MCC来自SIM卡,而MNC来自该设备将要附着的网络。你有时会仅使用MCC,例如包含特定国家合法资源在您的应用程序中。如果您的应用程序指定了MCC/MNC组合的资源,这些资源仅在MCCMNC都匹配的时候才能使用。

语言和区域Languageand region

两个字母的ISO639-1语言码和ISO3166-1-alpha-2区域码 ("r"为前缀)。比如en-rUS,fr-rFR,es-rES.这个代码是大小写敏感的:语言码是小写字母,国家码是大写字母。你不能单独指定一个区域,但是你可以单独指定一个语言,比如en,fr, es, zh.

屏幕方向Screenorientation

纵向,横向,正方形(port,land, square

屏幕像素密度Screenpixel density

92dpi,108dpiAndroid选择使用哪个资源时,它对屏幕像素密度的处理和其它限定符不同。在文章后面描述的步骤1Android如何查找最匹配的目录中,屏幕密度总被认为是匹配的。在步骤4中,如果被考虑的限定符是屏幕密度,Android将选择在那个位置的最佳匹配,而无需继续步骤5

触摸屏类型Touchscreentype

非触摸式,触摸笔,手指(notouch,stylus, finger

键盘可用方式Whetherthe keyboard is available to the user

外在键盘,隐藏键盘,软键盘(keysexposed,keyshidden,keyssoft
如果你的应用程序有一个特定的资源只能通过软件盘使用,则使用keyssoft 值,如果没有keyssoft 资源可用(只有keysexposed  keyshidden)并且该设备显示了一个软键盘,那么系统将使用keysexposed 资源。

首选文本输入方法Primarytext input method

不支持按键,标准键盘,12(nokeys,qwerty, 12key)

首选非触摸式导航方法Primarynon-touchscreen
navigation method

不支持导航,滑板,跟踪球,滚轮(nonav,dpad, trackball, wheel)

屏幕分辨率Screendimensions

320x240,640x480更大的分辨率必须先被指定。

SDK版本SDKversion

设备支持的SDK版本,比如v3Android1.0SDKv11.1SDKv21.5SDKv3

小版本(Minorversion)

你目前还不能指定小版本,它总是被设置为0

文件夹命名的各个部分是有顺序的。即按上表从上到下的优先级别排列,如:

drawable-en-rUS-land-mdpi
drawable-en-rUS-port-160dpi-finger-qwerty-dpad-480x320/(US代表美式英语)

举个使用过程中的例子:

任意建一个android项目。看一下res目录结构:


drawable-hdpi-finger与drawable-ldpi目录下面两张图片名称一样,但是图片不是同一张,分别是:


layout、layout-finger、layout-land-finger下面的main.xml文件内容分别是:

layout/main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   
  8. <ImageView  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:src="@drawable/icon"  
  12.     />   
  13. <Button  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content"  
  16.     android:background="@drawable/icon"  
  17.     />    
  18. <TextView   
  19.     android:layout_width="fill_parent"  
  20.     android:layout_height="fill_parent"  
  21.     android:text="layout only"/>   
  22. </LinearLayout>  
layout-finger/main.xml
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/icon"  
  11.         />   
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="@drawable/icon"  
  16.         />   
  17.     <TextView   
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="fill_parent"  
  20.         android:text="layout-finger"/>    
  21. </LinearLayout>  

layout-land-finger/main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/icon"  
  11.         />   
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="@drawable/icon"  
  16.         />   
  17.     <TextView   
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="fill_parent"  
  20.         android:text="layout-land-finger"/>    
  21. </LinearLayout>  
可以看出,他们区别在于TextView的显示内容不同,其它的一样。

运行App,在模拟器上显示效果


切换模拟器屏幕方向:


可以看出,显示的图片始终是,而不是。说明带有finger的文件夹优先级别较高。

layout-land-finger:适合电容屏、电阻屏以及非触摸屏的设备并且该设备是水平方向。

layout-finger:适合电容屏、电阻屏以及非触摸屏的设备但不要求是水平方向。

layout:适合电阻屏以及非触摸屏的设备。

另外,图片资源应该按照l:m:h = 3:4:6 的比例来设计,就是如果你只准备适合一种像素的drawable文件夹,如drawable-hdpi,并不是在低像素密度的手机上就不会显示图片或者报错的问题。因为资源名称是惟一的,在低像素密度手机上照样可以显示图片资源,只不过不好看而已,因为系统会自动调整hdi的大小来适配,如果系统想找一个ldpi(低密度)的资源,但找不到。那么系统会等比缩小hdpi的资源,为什么不寻找mdpi呢?因为系统对于hdpi更容易缩放,它的系数为0.5,相比mdpi的0.75来说。0.5的的性价比更高(方便计算,正确率也高一点)。关于这一点最好的证明就是android版本的QQ浏览器。你解压后发现它关于drawable就只有drawable和drawable-hdpi两个文件夹,看来腾讯里android的开发者应该很熟悉android系统规律了,因为默认的drawable 就是mdpi,只需要定义一套hdpi的drawable资源就行了。

当然,如果你想保证一些资源文件保持原样,尤其是图片不被拉伸或压缩,可以放入drawable-nodpi这个目录下

图片分辨率对应的文件夹

每部安卓手机屏幕都有一个初始的固定密度,这些数值是120、160、240、320、480,我们权且称为“系统密度”。大家发现规律没 有?相隔数值之间是2倍的关系。一般情况下,240×320的屏幕是低密度120dpi,即ldpi;320×480的屏幕是中密度160dpi,即 mdpi;480×800的屏幕是高密度240dpi,即hdpi;720×1280的屏幕是超高密度320dpi,即xhdpi;1080×1920的 屏幕是超超高密度480dpi,即xxhdpi。
安卓对界面元素进行缩放的比例依据正是系统密度,而不是实际密度。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值