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

标签 java android facebook-android-sdk android-resources

我正在尝试将 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 引用)?

最佳答案

我在这里回答这个问题,以防有人专门尝试将 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();
}

关于java - 如何动态加载 R.styleable 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15168018/

相关文章:

java - 使用 java.exe 的静默模式执行

android - 如何以编程方式设置微调器的样式?

android - 即使代码正常,也找不到启动器 Activity (尽管我不确定)

android - 如何在 Android 设备的根目录中创建/写入文件?

ionic-framework - 如何使用 Cordova 在应用中安装 Facebook Pixel 并跟踪转化?

java - 实体与模型

java - 使用构造函数创建基于参数化父类(super class)的生成器

java - FirstCup JavaEE 教程的问题

Android Facebook SDK - 与公众分享帖子

android - 通过 facebook android sdk 分享墙贴