java – 如何动态加载R.styleable资源?
发布时间:2020-05-24 01:35:25 所属栏目:Java 来源:互联网
导读:我正在尝试将Facebook Android SDK导出为JAR,以便在我的项目中使用. 这需要动态加载所有资源. 例如,我必须进行类似的更改: //findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.VISIBLE);int viewID = getResources().getIde
|
我正在尝试将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.我使用了这个问题的答案中描述的功能: 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:com / env-scheme在glassfish中查
- java – Apache Spark Lambda表达式 – 序列化问
- java – 调用返回null的getGraphics()的任何替代
- 使用Jackson来实现Java对象与JSON的相互转换的教
- springboot配置多数据源的实例(MongoDB主从)
- java – ServletContext对象的线程安全性
- java – JasperReport报告中的Excel单元格格式
- java使用分隔符连接数组中每个元素的实例
- java – @EnableTransactionManagement的范围是什
- cq5 – 如何从java use class hashmap中获取key
热点阅读
