2009-11-11 39 views

回答

69

this thread,R.styleable已經在Android 1.5或更高去除。

有許多的方式來獲得樣品的工作,我發現最簡單的是在連接到上面的線索推薦賈斯汀·安德森:

  1. 創建一個名爲「資源新的XML文件。 XML」,內容如下:

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <declare-styleable name="Gallery1"> 
         <attr name="android:galleryItemBackground" /> 
        </declare-styleable> 
    </resources> 
    
  2. 放入水庫XML文件\目錄值(旁邊的strings.xml)

  3. 最多日期與下面的ImageAdapter構造函數(假設ImageAdapter類是在它自己的文件中定義):

    public ImageAdapter(Context c) { 
        mContext = c; 
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1); 
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
        a.recycle(); 
    } 
    

該解決方案的設置樣式屬性基本上定義爲應用程序本身的資源,並賦予它的必要的結構在應用程序中工作。請注意,應用程序可以運行正常,如果你只是忽略的兩行代碼(前a.recycle();),所有這些代碼也被設置在圖庫中的圖像周圍灰色背景。

+4

我想幾千人一定有過這樣的問題。 – Ralphleon 2009-11-11 22:51:45

+1

是啊,我只好掏了一會兒後,我發現一個簡單的和可行的解決方案,所以我想我會在這裏發佈的SO,使其更容易爲鄉親找到。 – 2009-11-12 00:12:51

+1

謝謝你。我希望他們能更新教程! 順便說一句 - 我需要命名資源子元素declare-styleable,即styleable不可風化。 – 2009-12-23 12:19:13

11

這樣做的原因問題是,他們告訴你投入RES /價值/ attrs.xml的資源:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="HelloGallery"> 
     <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources> 

但你得到這個適配器,它的Eclipse想不通坦白地說沒有道理:

public ImageAdapter(Context c) { 
    mContext = c; 
    TypedArray a = obtainStyledAttributes(android.R.styleable.Theme); 
    mGalleryItemBackground = a.getResourceId(
      android.R.styleable.Theme_galleryItemBackground, 0); 
    a.recycle(); 
} 

這是因爲你不應該有「機器人」。在資源之前,風格化的名字是這裏的主題,但是在實際資源中的HelloGallery,以及galleryItemBackground把android風格化的名字和這樣的屬性放在一起:Theme_android_galleryItemBackground

所以,如果想讓ImageAdapter方法與資源一起工作,再給予,你應該重寫它是這樣的:

public ImageAdapter(Context c) { 
    mContext = c; 
    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
    mGalleryItemBackground = a.getResourceId(
      R.styleable.HelloGallery_android_galleryItemBackground, 0); 
    a.recycle(); 
} 

有關資源未來的問題(R. *解決不了類型錯誤),檢查/gen/R.java的資源實際上被命名什麼。

+0

@YumYumYum它的作品,它是正確的答案。 – 2016-01-05 16:45:50

1

有有選擇的回答有點錯誤,而不是與設置樣式

風格化的它應該是這樣的:

<declare-styleable name="Gallery1"> 
    <attr name="android:galleryItemBackground" /> 
</declare-styleable> 
+0

否 - 沒有工作,不正確的答案。 – YumYumYum 2013-07-25 11:31:25

5

一個稍微容易些,當然更MVCish的方法是使用樣式系統:

如果你沒有一個主題呢,創建一個styles.xmlres/values。在這裏面,你應該有:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="GalleryItem"> 
     <item name="android:background">?android:attr/galleryItemBackground</item> 
    </style> 
</resources> 

這將定義我們呼籲GalleryItem和設定任何的風格被應用到後臺資源的新風格,樣式屬性android:attr/galleryItemBackground的值(你可以看到在Android的源代碼中的frameworks/base/core/res/res/values/themes.xml中有很多這樣的例子)。

然後在一個ImageView的一個XML聲明,你可以簡單地通過增加style="@style/GalleryItem"應用您GalleryItem風格,如:

<?xml version="1.0" encoding="utf-8"?> 
<ImageView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/icon" 
    android:scaleType="fitXY" 
    android:layout_width="136dip" 
    android:layout_height="88dip" 
    style="@style/GalleryItem" 
/> 

這將讓你的風格的東西出你的適配器代碼(這是很好的!)並允許更多的通用適配器不需要關心如何將數據可視化。

+0

儘管如何將ImageView掛接到畫廊?該庫沒有任何在XML中聲明的內部元素。 – 2011-02-22 01:48:49

+0

是的,畫廊通過Adapters提供意見。您將需要創建一個適配器,從xml擴充ImageView。有關示例,請參閱ApiDemos/src/com/example/android/apis/view/Gallery2.java。 – 2011-02-22 21:42:10

+0

是的,這種方法更簡潔,即使是使用xml聲明的ImageView,而不是代碼創建的 – superjos 2011-06-15 21:09:01

3

設置樣式不支持http://developer.android.com/sdk/RELEASENOTES.html

的android.R.styleable類及其字段從公衆API中刪除,以更好地確保應用程序向前兼容。 android.R.styleable中聲明的常量是特定於平臺的,並且可以在各個版本之間進行任意更改,因此不適合應用程序使用。您仍然可以從資源或代碼訪問平臺的可修改屬性。要做到這一點,在申報項目的res/values/R.attrs文件中使用<declare-styleable>自定義資源元素,那麼內聲明的屬性。有關示例,請參見<sdk>/samples/ApiDemos/res/values/attrs.xml。有關自定義資源的更多信息,請參閱自定義佈局資源。需要注意的是android.R.styleable文檔在SDK還提供了,但只是作爲各種要素平臺的設置樣式屬性的參考。

1

我什麼都試過,但沒有運氣。生成的R.java顯示類風格的類,但彙編顯示「Stylable not found」。我在R之前剛剛添加的軟件包名稱,變更後,一切現在工作正常...

所以,如果你的包名是com.example.test,然後修改下面的代碼...

public ImageAdapter(Context c) { 
mContext = c; 
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1); 
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
a.recycle(); 

}

TO

public ImageAdapter(Context c) { 
    mContext = c; 
    TypedArray a = c.obtainStyledAttributes(com.example.test.R.styleable.Gallery1); 
    mGalleryItemBackground = a.getResourceId(com.example.test.R.styleable.Gallery1_android_galleryItemBackground, 0); 
    a.recycle(); 
} 
2

我有同樣的問題,我在谷歌

的自定義視圖示例代碼(餅圖)發現
import com.example.android.customviews.R; 

當我評論說,進口線,Eclipse會注意到錯誤:「R不能被解析爲一個變量」。所以你應該導入上面的包類似的聲明。例如:

import your.package.name.R; 

它修復類似的錯誤了我的其他項目