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複製到相應的目錄下,然後修改對應的尺寸值。 在運行時,系統會自動加載正確的目錄下的尺寸值,做到正確適配。

 

 

 

 

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。

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()