Android valuses
位于app/res/values文件是用來存放安卓資源的資源的文件其中包括四個個xml文件:

1. colors.xml 保存顏色資源
例如:
<resources> <!-- Base application theme. --> <style name="A2. dimens.xml 尺寸控制
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen></resources>dimens.xml(w820dp) 先解釋一下w820dp的意思,(可能是為電視設(shè)置的) 1. values-w820dp表明這個目錄下的資源所要求屏幕的最小寬度是820dp。 2. Samsung G920V的屏幕配置參數(shù):1440x2560像素,屏幕尺寸5.1寸,dpi=576.所以,Samsung G920V的最小屏幕寬度為:400dp (1440 * 160 / 576),因此不滿足w820dp的要求。
<resources> <!-- Example customization of dimensions originally defined in res/values/dimens.xml (such as screen margins) for screens with more than 820dp of available width. This would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). 示例自定義維度最初定義在res /價值/ dimens.xml (如屏幕邊緣)的屏幕寬度超過820 dp的可用。這 包括7“10”設(shè)備景觀(~ 960 dp ~ 960 dp分別)。--> <dimen name="activity_horizontal_margin">64dp</dimen></resources>3. string.xml
<resources> <string name="app_name">MyFirst</string></resources>4. style.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style></resources>引用方式舉例: 在代碼中通過 R.string.app_name 可以獲取app_name所對應(yīng)的字符串(MyFirst) 在XML中通過 @string/app_name 可以獲取app_name所對應(yīng)的字符串(MyFirst) (其他同理,相信聰明的你可以使用)
知其然,思其所以然 為什么要這么復(fù)雜? 這么復(fù)雜有卵用?
首先,這樣做不復(fù)雜,反而更加方便。倘若一個程序中好多地方用到了一個String,要是沒用這種全局常量的方式,需要一個一個地方改,反之則只需改一次,是不是方便了很多,雖然有的IDE有這個一改全改的功能,但是這是一種組件的開發(fā)思想。使用這種全局常量的方式也便于日后擴展復(fù)用等。
新聞熱點
疑難解答
圖片精選