標籤:android資源檔 資源檔 常用資源檔 bools style dimens
Android資源檔--values夾下檔案及用法
以下檔案皆在values檔案夾下。例如:全路徑:res/values/string.xml。
I>strings.xml檔案,定義一些需要在開發中使用的字串變數和數組,用來實現國際化,使用方法分別為:R.string.自己命名的名稱、@string/自己命名的名稱。
<resources> <!--屬性name="自己命名的名稱"--> <string name="app_name">Android4.0</string> <string_array name="ball"> <item name="basketball">籃球</item> <item name="soccer">足球</item></resources>
使用舉例:
在xxx.java檔案中使用方法:getResource().getString(R.string.app_name);
在xxx.xml檔案中使用方法:android:text=“@string/app_name”
II>array.xml檔案,定義存放一些數組的內容,使用方法同上。
<resources> <array name="color"> <item>#000</item> <item>#fff</item> </array></resources>
使用舉例:
在xxx.java檔案中使用方法:getResource().getStringArray(R.arry.color);
在xxx.xml檔案中使用方法:android:entries=“@array/color”(註:為spinner添加數組初始值)
III>colors.xml檔案,主要存放一些自訂的顏色,使用方法同上。
<resources> <color name="RED">#f00</color></resources>
使用舉例:
在xxx.java檔案中使用方法:getResource().getColor(R.color.RED);
在xxx.xml檔案中使用方法:android:background=“@color/RED”
IV>dimens.xml檔案,主要定義一些尺寸,使用方法同上。
<resources> <dimen name="horizontal_margin">15dp</dimen></resources>
使用舉例:
在xxx.xml檔案中使用方法:android:background=“@dimens/horizontal_margin”
尺寸的單位:
| 符號 |
名稱 |
用法 |
dp |
獨立像素 |
與裝置大小無關 |
| dx |
像素 |
與裝置大小無關 |
| sp |
放大像素 |
一般用於設定文字的大小 |
長度轉換:1 pt = 1/72 in
長度單位:
| 符號 |
名稱 |
| pt |
磅 |
| in |
英寸 |
| mm |
毫米 |
| cm |
厘米 |
V>bools.xml檔案,定義一個布爾型的檔案,使用方法同上。
<resources> <bool name="flag_on">true</bool></resources>
VI>styles.xml檔案,放置樣式的檔案,可以使自己定義的樣式,也可存放法系統的樣式,樣式可以應用用在視窗、控制項、布局、主題設定中,但是必須與控制項(View)的屬性保持一致。定義樣式分為兩種:
格式一:
<style name="定義當前的樣式/主題的名稱(主要用於引用)">
<item name="屬性名稱">屬性值</item>
</style>
<style name="mystyle"> <item name="android:layout_width">match_parent</item> <itme name="android:textSize">30sp</item></style>
使用舉例:
在xxx.xml檔案中使用方法:android:style=“@style/mystyle”
在資訊清單檔中使用:
<application
theme="@style/mystyle"
...>
...
</application>
...
格式二:
<style name="定義當前的樣式/主題的名稱(主要用於引用)" parent=“父樣式名稱”>
<item name="屬性名稱">屬性值</item>
</style>
<style name="mystylew2" parent="mystyle"> <item name="android:textcolor">#0f0</item></style><style name="mystyle.mystyle2"> </item name="android:textColor">#0f0</item></style>
使用舉例:
在xxx.xml檔案中使用方法:android:style=“@style/mystyle”
在資訊清單檔中使用:
<application
theme="@style/mystyle"
...>
...
</application>
...
總結:以上為values檔案夾下常用的檔案屬性的設定,還用其他的屬性,通常在屬性賦值的時候直接進行賦值,降低消耗,提高效率。常用的屬性通過調用名稱賦值,方便代碼的維護,簡化代碼,避免出現“重複造輪”的現象。以上都是自己命名並設定的屬性,其實android系統本身也為開發人員提供了一些屬性,例如:@android:color/darker_gray 調用系統提供的暗灰色。這一擷取方式僅僅擷取系統提供的顏色,樣式的擷取也可以採用這種方法式,至於詳細的講解,以後再繼續補充。希望對大家的學習和開發能有用,不足之處請大家不起賜教,謝謝。
本文出自 “In the eyes of the sun” 部落格,請務必保留此出處http://wang963825.blog.51cto.com/8695943/1864920
android常用的資源檔--values檔案夾內檔案