Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android从上下文获取资源';以编程方式创建主题(无需直接访问“R”)_Android - Fatal编程技术网

Android从上下文获取资源';以编程方式创建主题(无需直接访问“R”)

Android从上下文获取资源';以编程方式创建主题(无需直接访问“R”),android,Android,我将代码分为两个项目,以获得某种SDK,该SDK具有我未来所有项目所共有的功能 因此,我遇到了从一个项目的XML文件中从另一个项目中检索值的问题。例如,我必须在styles.xml中检索一种颜色,一种自定义颜色(我在attrs.xml中定义的,名为“iconColor”)。以下是我所做的: TypedValue typedValue = new TypedValue(); Resources.Theme theme = context.getTheme(); int colorIdentifie

我将代码分为两个项目,以获得某种SDK,该SDK具有我未来所有项目所共有的功能

因此,我遇到了从一个项目的XML文件中从另一个项目中检索值的问题。例如,我必须在styles.xml中检索一种颜色,一种自定义颜色(我在attrs.xml中定义的,名为“iconColor”)。以下是我所做的:

TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
int colorIdentifier = context.getResources().getIdentifier("iconColor", "attr", context.getPackageName());
if (theme.resolveAttribute(colorIdentifier, typedValue, true)) {
    return typedValue.data;
}
else {
    return 0;
}
从一个项目到另一个项目运作良好。现在出于另一个目的,我想检索上下文主题的colorPrimary。我调整了我的代码,使其具有一个通用函数,现在是这样的:

/**
 * Retrieves a resource from context's theme using the resource id
 *
 * @param resId the resource id, ie. android.R.attr.colorPrimary
 * @return the resource value, or null if not found
 */
public @Nullable Integer getResourceFromTheme(int resId) {
    Integer result = null;

    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    if (theme.resolveAttribute(resId, typedValue, true)) {
        result = typedValue.data;
    }

    return result;
}

/**
 * Retrieves a resource from context's theme using the resource name
 *
 * @param resName the resource name, ie. "colorPrimary"
 * @return the resource value, or null if not found
 */
public @Nullable Integer getResourceFromTheme(@NonNull String resName) {
    int resId = context.getResources().getIdentifier(resName, "attr", context.getPackageName());
    return getResourceFromTheme(resId);
}
因此,理论上,我可以使用android.R检索自定义属性和本机属性。但是,如果它仍然适用于我的costom iconColor属性,我无法检索colorPrimary,它返回的数据不正确。更奇怪的是,当我尝试使用一种或另一种方法时,我得到了不同的值,这意味着我得到了错误的代码:


好的,所以函数工作正常,但我犯了两个错误,xxxx是ColorRes,aaaa/bbbb是ColorInt,我用android.R.attr.colorPrimary而不是R.attr.colorPrimary调用我的函数