我有一个简短的问题:
如何转换包含Drawable的Id的String,即
String idString = "R.drawable.bubblegum";
Run Code Online (Sandbox Code Playgroud)
到整数,
idInt
Run Code Online (Sandbox Code Playgroud)
这样我就可以使用该ID作为图像的参考(在SimpleAdapter中使用)
所以,举个例子,我做不到:
bubble.setImageDrawable(this.getResources().getDrawable(idString));
//not possible, cause idString is a String an not an Id/Int
Run Code Online (Sandbox Code Playgroud)
所以,我有包含id的String,但不幸的是作为String.
谢谢!
dom*_*som 21
虽然这个问题已经相当陈旧,但你所缺少的是"id"和"drawable"是不同的资源类型.而不是
getResources().getIdentifier(stringId, "id", "my.Package");
Run Code Online (Sandbox Code Playgroud)
它的
getResources().getIdentifier(stringId, "drawable", "my.Package");
Run Code Online (Sandbox Code Playgroud)
您还可以使用活动上下文获取包名称 activityContext.getPackageName()
/**
* Returns Identifier of String into it's ID as defined in R.java file.
* @param pContext
* @param pString defnied in Strings.xml resource name e.g: action_item_help
* @return
*/
public static int getStringIdentifier(Context pContext, String pString){
return pContext.getResources().getIdentifier(pString, "string", pContext.getPackageName());
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 13
调用您获得getIdentifier()
的Resources
对象getResources()
,如以下StackOverflow问题所示:
等等.
10f*_*0ff -2
至少,我无法找到解决这个问题的方法。似乎没有办法将字符串“R.id.mytext”转换为像 R.id.mytext 这样可在 findViewById(R.id.myText) 中使用的整数。
归档时间: |
|
查看次数: |
16108 次 |
最近记录: |