ch3*_*0ke 5 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);
Run Code Online (Sandbox Code Playgroud)
注释行显示原始行,下面的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();
}
Run Code Online (Sandbox Code Playgroud)
然后在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>
Run Code Online (Sandbox Code Playgroud)
如何动态加载此资源(例如替换R.styleable引用)?
我在这里回答这个问题是为了防止有人专门尝试将 Facebook SDK 作为 jar 导出。
我使用了这个问题的答案中描述的函数: Accessing <declare-styleable> resourcesprogramatically
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2157 次 |
| 最近记录: |