解密Android Resource

類型

Resource有一下幾種類型:
1、Animation Resources:包括屬性動畫、視圖動畫(補間動畫、幀動畫)。屬性動畫xm文件l在res/animator/目錄下,如res/animator/filenam.xml;補間動畫xml文件在目錄res/anim/目錄下,幀動畫xml文件在res/drawable/目錄下。
2、Color State List Resource:xml文件在res/color/目錄下
3、Drawable Resources:圖片或xml問價在res/drawable/目錄下
4、Layout Resource:xml文件在res/layout/目錄下
5、Menu Resource:xml文件在res/menu/目錄下
6、String Resources:xml文件在res/values/目錄下
7、Style Resource:xml文件在res/values/目錄下
8、其他類型:包括Bool、Color、ID(ID在xml文件中聲明,非Layout的xml文件)、Dimension等

使用

Resource可以在xml中引用,也可以在代碼中引用,使用方式有所不同。
1、在xml文件中使用:在xml文件中引用resource,語法“@[package:]type/name"。如引用一個TextView“@id/textView”,無指明package表示引用app內資源
2、在代碼中使用:通過TextView的id(R.id.textView),獲取TextView對象的引用
3、通過屬性應用resource

<?xml version="1.0" encoding="utf-8"?> 
<TextView id="text" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:textColor="?android:attr/actionMenuTextColor" 
    android:text="@string/hello_world" /> 

android框架內的themes.xml文件中<style name="Theme">以及子theme預先定義了許多屬性值。這些屬性值可以通過?android:attr/name的方式來引用。

原理

對於每個Resource都可以用一個整數來關聯,對於Layout Resource、Drawable Resource等Resource SDK工具會在R.class自動生成一個整數對應Resource,整數的引用通常是文件名。

android:id="@+id/resource_name"這種方式是給設置控件一個ID,同時,如果R.class文件中沒有聲明resource_name,則會創建resource_name的int。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章