android的資源是您在代碼中使用到的並且在編譯時間被打包進您的應用程式的附加檔案。Android支援多種不同的檔案,包括XML、PNG和JPEG檔案。XML檔案的格式決定於其描述的內容。這些檔案將描述檔案支援的類型、文法或格式。處於載入效率的考慮,資源被從代碼中分離出來,而且XML檔案被編譯進二進位代碼中。字串,類似的會被壓縮儲存為更經濟的格式。處於這樣或那樣的原因,Android平台中會存在很多不同的資源和資源定義檔案。
Android資源系統儲存所有與代碼無關資源的存根。您可以使用Resources類訪問您應用程式的資源;
與應用程式相關聯的資源執行個體可以通過Context.getResources()得到。
一、建立資源
Android支援字串,位元影像和許多其他類型的資源。每一種資源定義檔案的文法和格式及儲存的位置取決於其依賴的對象。
通常,您可以通過三種檔案建立資源:XML檔案(除位元影像和原生檔案外),位元影像檔案(作為圖片)和原生檔案(所有其他的類型,比如音效檔)。
事實上,這裡有兩種不同類型的XML檔案,一種是作為資源被編譯進應用程式,另一種是關於資源的描述,被aapt使用。
您可以在您項目res/目錄下的適當子目錄下建立和儲存資源檔。Android使用資源編譯器訪問資源所在的子目錄和格式化的檔案。
下面的表格列出了每一種資源的檔案類型
res/anim目錄
XML檔案編譯為楨序列動畫或者自動動畫對象。
res/drawable目錄
.png,9.png,.jpg檔案被編譯為Drawable資源子類型:
使用Resources.getDrawable(id)可以獲得資源類型
* 位元影像檔案:png,jpg等普通圖片檔案
* 9-patchs(可變位元影像檔案):9.png檔案
*selector
它主要定義控制項在下pressed,selected,focused及平常情況下的屬性
更詳細參見《
selector的使用》
res/layout目錄
資源編譯為螢幕布局器.
res/values目錄
XML 檔案可以被編譯為多種資源
注意:不像其他res下的目錄,這個目錄可以包含多個資源描述檔案。XML檔案元素類型控制著這些資源被R類放置在何處。
這些檔案可以自訂名稱。這裡有一些約定俗成的檔案。
* arrays.xml 定義數組。
參見附樣本1
* colors.xml 定義可繪製對象的顏色和字串的顏色。使用Resources.getDrawable()和Resources.getColor()都可以獲得這些資源。
參見附樣本2
* dimens.xml 定義尺度。使用Resources.getDimension()可以獲得這些資源
參見附樣本3
* strings.xml 定義字串(使用Resources.getString()或者更適合的Resources.getText()方法獲得這些資源。
Resources.getText()方法將保留所有用於描述使用者介面樣式的描述符,保持複雜文本的原貌。
參見附樣本4
* styles.xml 定義樣式對象。
styles就是屬性集合。
更詳細參見《
styles的使用》
*attrs.xml 定義控制項對象的屬性。
更詳細參見《
自訂控制項屬性(attr.xml,TypedArray)》
*themes.xml
詳細參見《
風格和主題(style,themes)》
res/xml目錄
自訂的XML檔案。這些檔案將在運行時編譯近應用程式,並且使用Resources.getXML()方法可以在運行時擷取。
res/raw
自訂的原生資源,將被直接拷貝入裝置。這些檔案將不被壓縮近您的應用程式。
使用帶有ID參數的Resources.getRawResource()方法可以獲得這些資源,比如R.raw.somefilename。
資源被最終編譯進APK檔案。Android建立封裝類R,您可以用他找回資源。R包含一些與資源所在目錄同名的子類
很多資源都允許您定義顏色。Android接受web風格的顏色定義-一組十六進位的顏色值,按照以下序列排列:#RGB,#ARGB,#RRGGBB,#AARRGGBB。
*所有的顏色都支援ALPHA通道,頭兩位十六進位數字指定透明度。0在ALPHA通道中表示全透明,預設值是不透明。
在編譯時間,Android產生名為R的類。R包含您應用程式所用到的所有資源的索引。這個類包含一些與res下子目錄同名的子類。
這些子類包含每一個您在資源檔中定義的資源的標識。這些資源標識可以在您的代碼中引用。
注意:R類是自動產生的,並且它不能被手動修改。當資源發生變動時,它會自動修改。
以下是一個產生的R類的樣本:
package com.android.samples;
public final class R {
public static final class string {
public static final int greeting=0x0204000e;
public static final int start_button_text=0x02040001;
public static final int submit_button_text=0x02040008;
public static final int main_screen_title=0x0204000a;
};
public static final class layout {
public static final int start_screen=0x02070000;
public static final int new_user_pane=0x02070001;
public static final int select_user_list=0x02070002;
};
public static final class drawable {
public static final int company_logo=0x02020005;
public static final int smiling_cat=0x02020006;
public static final int yellow_fade_background=0x02020007;
public static final int stretch_button_1=0x02020008;
};
};
二、在代碼中使用資源
在代碼中使用資源需要知道完整的資源ID和您的資來源物件類型。下面是資源引用文法:
R.resource_type.resource_name (引用當前應用程式的資源)或者 android.R.resource_type.resource_name(引用android系統的資源)
resource_type是R類中儲存制定類型資源的子類。resource_name是定義在XML檔案中的資源名或者被其他檔案類型所定義的資源檔名(無副檔名)每一類型的資源都依據其類型,被添加入某一指定的R子類;
引用當前應用程式可以不帶包名(比如R.resource_type.resource_name)。
Android包含一個標準資源的序號,比如螢幕的樣式和按鈕的背景。引用這些資源,您必須使用帶android的文法,比如android.R.drawable.button_background。
樣本1:
// Load a background for the current screen from a drawable resource.
this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
// WRONG Sending a string resource reference into a
// method that expects a string.
this.getWindow().setTitle(R.string.main_title);
// RIGHT Need to get the title from the Resources wrapper.取字串
this.getWindow().setTitle(Resources.getText(R.string.main_title));
// Load a custom layout for the current screen.
setContentView(R.layout.main_screen);
// Set a slide in animation for a ViewFlipper object.
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.hyperspace_in));
// Set the text on a TextView object.
TextView msgTextView = (TextView)findViewByID(R.id.msg);
msgTextView.setText(R.string.hello_message);
三、引用資源
一個屬性值(或資源)同樣可以引用資源。這種用法常在資源布局器檔案中用於文字和圖片(定義在其他檔案中)。
這種方法可以引用任何資源,包括顏色和整數。
比如,如果我們有一個顏色資源,我們可以寫一個布局器檔案,在其中指定文本顏色和尺寸。
樣本2:
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="Hello, World!" />
注意:“@”首碼聲明這是一個資源引用—隨後的文本是以@[package:]type/name形式提供的資源名。在這個例子中我們不需要指明特定的包,因為我們在我們自己的包中引用。
四、引用系統資源
引用一個系統資源時,就是@android:type/name的形式。比如樣本3.
樣本3:
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@android:color/opaque_red"
android:text="Hello, World!" />
五、使用系統資源
許多包含於系統之中的資源是能被應用程式所訪問的。所有的資源被定義在android.R類中。
比如,您可以使用一下代碼在螢幕上顯示標準應用程式的ICON
樣本5:
public class MyActivity extends Activity
{
public void onStart()
{
requestScreenFeatures(FEATURE_BADGE_IMAGE);
super.onStart();
setBadgeResource(android.R.drawable.sym_def_app_icon);
}
}
類似的,下面的代碼會更改您的系統主題
樣本6:
public class MyActivity extends Activity
{
public void onStart()
{
super.onStart();
setTheme(android.R.style.Theme_Black);
}
}
六、附件
附樣本1:
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="twarr_indexlist">
<!-- <item>!@#</item> -->
<!-- <item>123</item> -->
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>
<string-array name="arr_indexlist_clock">
<item>+13</item>
<item>+12</item>
<item>+11</item>
</string-array>
</resources>
附樣本2:
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent_color">#00000000</color>
</resources>
附樣本3:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="test_textsize_default">28sp</dimen>
<dimen name="test_itemheight_default">63dp</dimen>
</resources>
附樣本4:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World,\n Hello!</string>
<string name="hi">Hi,hubin!</string>
</resources>