Asw*_*wan 168 java android kotlin android-resources
我想通过名称而不是其int id访问像String或Drawable这样的资源.
我会用哪种方法?
Mar*_*ues 317
如果我理解正确,这就是你想要的
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
Run Code Online (Sandbox Code Playgroud)
"this"是一个活动,只是为了澄清.
如果您想要strings.xml中的String或UI元素的标识符,请替换"drawable"
int resourceId = this.getResources().getIdentifier("nameOfResource", "id", this.getPackageName());
Run Code Online (Sandbox Code Playgroud)
我警告你,这种获取标识符的方式非常慢,只在需要的地方使用.
链接到官方文档:Resources.getIdentifier(String name,String defType,String defPackage)
Rab*_*bid 140
它将是这样的:
R.drawable.resourcename
确保没有Android.R导入名称空间,因为它可能会混淆Eclipse(如果你正在使用它).
如果这不起作用,您可以始终使用上下文的getResources方法...
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
Run Code Online (Sandbox Code Playgroud)
凡this.context被intialised作为Activity,Service或任何其他Context子类.
更新:
如果它是你想要的名字,那么这个Resources类(返回者getResources())有一个getResourceName(int)方法,一个getResourceTypeName(int)?
更新2:
本Resources类有这个方法:
public int getIdentifier (String name, String defType, String defPackage)
Run Code Online (Sandbox Code Playgroud)
返回指定资源名称的整数,类型和包.
小智 25
int resourceID =
this.getResources().getIdentifier("resource name", "resource type as mentioned in R.java",this.getPackageName());
Run Code Online (Sandbox Code Playgroud)
小智 13
从字符串获取资源ID的简单方法.这里resourceName是drawable文件夹中资源ImageView的名称,它也包含在XML文件中.
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable",
context.getPackageName());
im.setImageResource(id);
Run Code Online (Sandbox Code Playgroud)
ami*_*phy 11
Kotlin Version通过Extension Function要按名称查找资源ID在Kotlin中,将以下代码段添加到kotlin文件中:
ExtensionFunctions.kt
import android.content.Context
import android.content.res.Resources
fun Context.resIdByName(resIdName: String?, resType: String): Int {
resIdName?.let {
return resources.getIdentifier(it, resType, packageName)
}
throw Resources.NotFoundException()
}
Run Code Online (Sandbox Code Playgroud)
Usage现在,无论您使用resIdByName哪种方法使用上下文引用,都可以访问所有资源ID :
val drawableResId = context.resIdByName("ic_edit_black_24dp", "drawable")
val stringResId = context.resIdByName("title_home", "string")
.
.
.
Run Code Online (Sandbox Code Playgroud)
// image from res/drawable
int resID = getResources().getIdentifier("my_image",
"drawable", getPackageName());
// view
int resID = getResources().getIdentifier("my_resource",
"id", getPackageName());
// string
int resID = getResources().getIdentifier("my_string",
"string", getPackageName());
Run Code Online (Sandbox Code Playgroud)
我建议你使用我的方法来获取资源ID.它比使用缓慢的getIdentidier()方法更有效.
这是代码:
/**
* @author Lonkly
* @param variableName - name of drawable, e.g R.drawable.<b>image</b>
* @param ? - class of resource, e.g R.drawable.class or R.raw.class
* @return integer id of resource
*/
public static int getResId(String variableName, Class<?> ?) {
Field field = null;
int resId = 0;
try {
field = ?.getField(variableName);
try {
resId = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resId;
}
Run Code Online (Sandbox Code Playgroud)