Android提供了一套资源命名和编译机制,方便开发者适配多种手机屏幕和分辨率。
今天来讲一下如何将界面布局中的固定尺寸值适配至多种手机屏幕和分辨率。
界面布局中要做到良好的适配,最好不要出现固定的尺寸值,如果无法避免,那么我们怎样让这一固定尺寸值在不同手机屏幕和分辨率下进行相应变化呢?
使用res/values/dimens.xml来解决这一问题!
以下是一个示例dimens.xml:
<resources> <dimen name="button_margin_top">15dp</dimen> <dimen name="text_margin_top">10dp</dimen> </resources>
在布局xml文件中,不要直接写死尺寸数值,而是用 "@dimen/xxx"来引用。
如果想在不同的手机屏幕大小和分辨率上改变这一值,可以创建不同的values-qualifier目录,将dimens.xml复制到相应的目录下,然后修改对应的尺寸值。 在运行时,系统会自动加载正确的目录下的尺寸值,做到正确适配。
不同的qualifier有以下几种:(详见 http://developer.android.com/guide/practices/screens_support.html#qualifiers)
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. | |
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). | |
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. | |
Orientation | land | Resources for screens in the landscape orientation (wide aspect ratio). |
port | Resources for screens in the portrait orientation (tall aspect ratio). | |
Aspect ratio | long | Resources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration. |
notlong | Resources for use screens that have an aspect ratio that is similar to the baseline screen configuration.
|
一些示例:
res/layout/my_layout.xml // layout for normal screen size ("default") res/layout-small/my_layout.xml // layout for small screen size res/layout-large/my_layout.xml // layout for large screen size res/layout-xlarge/my_layout.xml // layout for extra large screen size res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation res/drawable-mdpi/my_icon.png // bitmap for medium density res/drawable-hdpi/my_icon.png // bitmap for high density res/drawable-xhdpi/my_icon.png // bitmap for extra high density
对了,有一个关于屏幕大小的定义:
- xlarge screens are at least 960dp x 720dp
- large screens are at least 640dp x 480dp
- normal screens are at least 470dp x 320dp
- small screens are at least 426dp x 320dp
这里我们要创建的是values目录,可以创建以下目录:
values-small
426dp x 320dp以上,470dp x 320dp 以下
values-normal
470dp x 320dp 以上, 640dp x 480dp以下
values-large
640dp x 480dp以上
values-xlarge
960dp x 720dp以上
在项目实作中,遇到一个问题,要对HVGA(320 * 480 px, density = 160), WVGA(480*800 px, density = 240) 和 QHD (540*960 px, density = 240) 进行适配, 换算成dp后,可以看到三种分辨率都是属于values-normal范围的,这下麻烦了,这三种屏幕尺寸差异很大,界面适配上必须要对应不同的值,怎么办呢?
解决方法很简单,实际上qualifier是可以配置多个的,之间以"-"连接。
将values-normal 分成两个目录,即values-normal-mdpi, values-normal-hdpi, 其下分别对应相应的dimens.xml即可。values-normal-mdpi 对应 HVGA, values-normal-hdpi对应WVGA和QHD。
如何进一步分开适配WVGA和QHD,还没有找到方法,有知道的朋友麻烦留言告诉一下,多谢。