【Android細節(一)】Gallery設置自定義的背景屬性


問題:在我們使用Gallery作爲前端佈局,需要

   1、先使用TypeArray自定義屬性類

   2、再通過TypeArray對象調用getResourceId()得到android:GalleryBackground或android:GalleryItemBackground格式。

但是android:GalleryBackground或android:GalleryItemBackground 這個格式在android包內本身依然存在,爲什麼不直接調用android:GalleryBackground或android:GalleryItemBackground格式呢?

是因爲在創建TypeArray對象時,android:R.styleable在SDK1.5中已經不再支持。

所以需要我們創建R.styleable.Gallery這個自定義屬性:

<span style="font-size:18px;"> <?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="Gallery">  
        <attr name="android:galleryItemBackground">  
        </attr>  
    </declare-styleable>  
</resources></span>
然後再生成android:GalleryBackground 或 android:GalleryItemBackground這兩種系統所給屬性的GalleryBackground

<span style="font-size:18px;">    public ImageAdapter(Context c) {  
                mContext = c;  
                TypedArray a = obtainStyledAttributes(R.styleable.Gallery);  
                mGalleryItemBackground = a.getResourceId(  
                        R.styleable.Gallery_android_galleryItemBackground, 0);  
                a.recycle();  
            }  </span>
謝謝~

發佈了61 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章