首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何动态加载R.styleable资源?

如何动态加载R.styleable资源?
EN

Stack Overflow用户
提问于 2013-03-02 06:36:37
回答 1查看 2.3K关注 0票数 5

我正在尝试将Facebook Android SDK导出为JAR,以便在我的项目中使用。

这需要动态加载所有资源。

例如,我必须进行类似如下的更改:

代码语言:javascript
复制
//findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.VISIBLE);
int viewID = getResources().getIdentifier("com_facebook_login_activity_progress_bar", "id", getPackageName());
findViewById(viewID).setVisibility(View.VISIBLE);

注释行显示了原始资源,下面的两行显示了我为动态加载相同资源所做的更改。

Facebook SDK声明了一个R.styleable资源,我不知道如何动态加载它。以下是原始代码:

代码语言:javascript
复制
private void parseAttributes(AttributeSet attrs) {
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_profile_picture_view);
    setPresetSize(a.getInt(R.styleable.com_facebook_profile_picture_view_preset_size, CUSTOM));
    isCropped = a.getBoolean(R.styleable.com_facebook_profile_picture_view_is_cropped, IS_CROPPED_DEFAULT_VALUE);
    a.recycle();
}

然后在attrs.xml中声明以下内容:

代码语言:javascript
复制
    <declare-styleable name="com_facebook_profile_picture_view">
        <attr name="preset_size">
            <!-- Keep in sync with constants in ProfilePictureView -->
            <enum name="small" value="-2" />
            <enum name="normal" value="-3" />
            <enum name="large" value="-4" />
        </attr>
        <attr name="is_cropped" format="boolean" />
    </declare-styleable>

如何动态加载此资源(例如,替换R.styleable引用)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-02 07:10:11

我在这里回答这个问题,以防任何人特别尝试将Facebook SDK导出为jar。

我使用了这个问题的答案中描述的函数:Accessing resources programatically

代码语言:javascript
复制
private void parseAttributes(AttributeSet attrs) {
    int attrArray[] = StyleableHelper.getResourceDeclareStyleableIntArray(getContext(), "com_facebook_profile_picture_view");
    //TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_profile_picture_view);
    TypedArray a = getContext().obtainStyledAttributes(attrs, attrArray);

    setPresetSize(a.getInt(0, CUSTOM));
    isCropped = a.getBoolean(1, IS_CROPPED_DEFAULT_VALUE);
    //setPresetSize(a.getInt(R.styleable.com_facebook_profile_picture_view_preset_size, CUSTOM));
    //isCropped = a.getBoolean(R.styleable.com_facebook_profile_picture_view_is_cropped, IS_CROPPED_DEFAULT_VALUE);
    a.recycle();
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15168018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档