加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:34:17 所属栏目:Java 来源:网络整理
导读:我正在尝试将Facebook Android SDK导出为JAR,以便在我的项目中使用. 这需要动态加载所有资源. 例如,我必须进行类似的更改: //findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.VISIBLE);int viewID = getResources().getId
我正在尝试将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);

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

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

解决方法

我正在回答这里的问题,因为任何人都特意尝试将Facebook SDK导出为jar.

我使用了这个问题的答案中描述的功能:
Accessing <declare-styleable> resources programatically

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读