欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 中SP與DP的區(qū)別實(shí)例詳解

 更新時(shí)間:2016年10月10日 11:59:18   投稿:lqh  
這篇文章主要介紹了Android 中SP與DP的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

從一開(kāi)始寫(xiě)Android程序,就被告知這些常識(shí)

1.長(zhǎng)度寬度的數(shù)值要使用dp作為單位放入dimens.xml文件中

2.字體大小的數(shù)值要使用sp作為單位,也放入dimens.xml文件中

然后,就沒(méi)有然后了,仿佛潛臺(tái)詞就是說(shuō),你記住去用就行了.

偶然有一天,當(dāng)我們陰差陽(yáng)錯(cuò)地將字體寫(xiě)成了dp,也是可以工作,而且效果和sp一樣.

這時(shí)候,就開(kāi)始懷疑了,到底有啥區(qū)別呢,dp和sp有什么不同呢?

我們做個(gè)簡(jiǎn)單的Sample驗(yàn)證一下,如下,一個(gè)布局代碼

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="18sp"
   android:text="Hello World! in SP" />

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="18dp"
   android:text="Hello World! in DP" />

得到的效果是這個(gè)樣子

但是,當(dāng)我們進(jìn)入系統(tǒng)設(shè)置中修改字體大小時(shí)

再次進(jìn)入之前的界面,發(fā)現(xiàn)了一些不一樣的東西.

由此看來(lái)

使用sp作為字體大小單位,會(huì)隨著系統(tǒng)的字體大小改變

而dp作為單位則不會(huì).

關(guān)于sp,文檔的描述為:

Scale-independent Pixels – This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

大致意思為

sp除了受屏幕密度影響外,還受到用戶(hù)的字體大小影響

通常情況下,建議使用sp來(lái)跟隨用戶(hù)字體大小設(shè)置

因此通常情況下,我們還是建議使用sp作為字體的單位,除非一些特殊的情況,不想跟隨系統(tǒng)字體變化的,可以使用dp.

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論