访问<申报,设置样式>资源编程 [英] Accessing <declare-styleable> resources programatically

查看:72
本文介绍了访问<申报,设置样式>资源编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我找不到任何帮助:

I've one question where I couldn't find any help:

是否有可能,以获得资源-IDS中保留了一个为int []编程,而不指的是资源型R级?

Is it possible, to receive the resource-ids being kept by a as an int[] programatically without refering to the resource-class R ?

<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>

现在的问题是,我不能化解定义的申报,设置样式'属性的ID - 为0x00总是返回:

The problem is that I cannot resolve the ID of the defined 'declare-styleable' attribute - 0x00 is always returned:

int id = context.getResources().getIdentifier( "com_facebook_login_view", "declare-styleable", context.getPackageName() ); 
int[] resourceIDs = context.getResources().getIntArray( id );

任何想法会大大AP preciated! :)

Any idea would be greatly appreciated! :)

在此先感谢!

克里斯托弗

推荐答案

下面是提供的资源标识编程为孩子解决方案 - 为标签定义的标记:

Here is the solution that delivers the resource-IDs programatically for the child--tags defined for a tag:

/*********************************************************************************
*   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;
}

也许这可以帮助别人一天。 :)

Maybe this could help somebody one day. :)

问候

克里斯托弗

这篇关于访问&LT;申报,设置样式&GT;资源编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆