您的位置:首页 > 移动开发 > Android开发

【Android细节(一)】Gallery设置自定义的背景属性

2015-03-22 22:07 621 查看
问题:在我们使用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>
谢谢~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: