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

5

我正在尝试将Facebook Android SDK导出为JAR文件以供我的项目使用。

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

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

//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资源,我无法弄清楚如何动态加载它。以下是原始代码:

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中,声明如下:

    <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引用)?

1
查看以编程方式访问<declare-styleable>资源的帖子,可能会帮助您解决当前的问题。 - ρяσѕρєя K
太棒了,感谢您的快速回复。那正是我所需要的。如果您将其发布为答案,我会打勾标记它! - ch3rryc0ke
1个回答

3

如果有人特别想将Facebook SDK导出为jar文件,我在这里回答问题。

我使用了这个问题答案中描述的函数: 以编程方式访问<declare-styleable>资源

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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接