1. 程式人生 > >引用系統資源 error Error Resource is not public

引用系統資源 error Error Resource is not public

               

本文轉自: http://www.cnblogs.com/vieboo/archive/2013/05/30/3108130.html

宣告: 1)利用系統定義的id 

<ListView

    android:id="@android:id/list"

使用android.R.id.list來獲取。

3)利用系統的字串資源 @android:string/yes和@android:string/no,在簡體中文環境下會顯示“確定”和“取消”,在英文環境下會顯示“OK”和“Cancel

 4)利用系統的Style

android:textAppearance="?android:attr/textAppearanceMedium"就是使用系統的style。需要注意的是,使用系統的style,需要在想要使用的資源前面加“?android:”作為字首,而不是“@android:”。

5)利用系統的顏色定義

android:background ="@android:color/transparent"

Android系統本身有很多資源在應用中都可以直接使用,具體的,可以進入android-sdk的相應資料夾中去檢視。例如:可以進入$android-sdk$\platforms\android-8\data\res,裡面的系統資源就一覽無餘了。

2)利用系統的圖片資源

    假設我們在應用程式中定義了一個menu,xml檔案如下。

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/menu_attachment"

        android:title="附件"

        android:icon="@android:drawable/ic_menu_attachment" />

</menu>

其中程式碼片段android:icon="@android:drawable/ic_menu_attachment"本來是想引用系統中已有的Menu裡的“附件”的圖示。但是在Build工程以後,發現出現了錯誤。提示資訊如下:

error: Error: Resource is not public. (at 'icon' with value '@android:drawable/ic_menu_attachment').

從錯誤的提示資訊大概可以看出,由於該資源沒有被公開,所以無法在我們的應用中直接引用。既然這樣的話,我們就可以在Android SDK中找到相應的圖片資源,直接拷貝到我們的工程目錄中,然後使用類似android:icon="@drawable/ic_menu_attachment"的程式碼片段進行引用。

這樣做的好處,一個是美工不需要重複的做一份已有的圖片了,可以節約不少工時;另一個是能保證我們的應用程式的風格與系統一致。

經驗分享:

Android中沒有公開的資源,在xml中直接引用會報錯。除了去找到對應資源並拷貝到我們自己的應用目錄下使用以外,我們還可以將引用“@android”改成“@*android”解決。比如上面引用的附件圖示,可以修改成下面的程式碼。

android:icon="@*android:drawable/ic_menu_attachment"

修改後,再次Build工程,就不會報錯了。