首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式访问<declare-styleable>资源

以编程方式访问<declare-styleable>资源
EN

Stack Overflow用户
提问于 2012-12-11 16:49:48
回答 3查看 4.4K关注 0票数 6

是否有可能在不引用资源类R的情况下以编程方式接收由作为int[]保存的资源it?

代码语言:javascript
复制
<declare-styleable name="com_facebook_login_view">
    <attr name="confirm_logout" format="boolean"/>
    <attr name="fetch_user_info" format="boolean"/>
    <attr name="login_text" format="string"/>
    <attr name="logout_text" format="string"/>
</declare-styleable>

问题是我无法解析定义的'declare-styleable‘属性的ID --总是返回0x00:

代码语言:javascript
复制
int id = context.getResources().getIdentifier( "com_facebook_login_view", "declare-styleable", context.getPackageName() ); 
int[] resourceIDs = context.getResources().getIntArray( id );
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-12 22:05:05

以下是以编程方式为为<declare-styleable>标记定义的child-<attr>-tags提供资源is的解决方案:

代码语言:javascript
复制
/*********************************************************************************
*   Returns the resource-IDs for all attributes specified in the
*   given <declare-styleable>-resource tag as an int array.
*
*   @param  context     The current application context.
*   @param  name        The name of the <declare-styleable>-resource-tag to pick.
*   @return             All resource-IDs of the child-attributes for the given
*                       <declare-styleable>-resource or <code>null</code> if
*                       this tag could not be found or an error occured.
*********************************************************************************/
public static final int[] getResourceDeclareStyleableIntArray( Context context, String name )
{
    try
    {
        //use reflection to access the resource class
        Field[] fields2 = Class.forName( context.getPackageName() + ".R$styleable" ).getFields();

        //browse all fields
        for ( Field f : fields2 )
        {
            //pick matching field
            if ( f.getName().equals( name ) )
            {
                //return as int array
                int[] ret = (int[])f.get( null );
                return ret;
            }
        }
    }
    catch ( Throwable t )
    {
    }

    return null;
}

也许有一天这能帮上忙。

票数 15
EN

Stack Overflow用户

发布于 2017-02-02 07:33:56

更高效的解决方案:

代码语言:javascript
复制
public static final int[] getResourceDeclareStyleableIntArray(String name) {
        Field[] allFields = R.styleable.class.getFields();
        for (Field field : allFields) {
            if (name.equals(field.getName())) {
                try {
                    return (int[]) field.get(R.styleable.class);
                } catch (IllegalAccessException ignore) {}
            }
        }

        return null;
    }
票数 1
EN

Stack Overflow用户

发布于 2018-10-15 12:32:15

代码语言:javascript
复制
public static final int[] getResourceDeclareStyleableIntArray(String name) {
    int[] result = null;
    try {
        result = (int[]) R.styleable.class.getField(name).get(R.styleable.class);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return result;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13816596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档