【发布时间】:2012-06-01 05:29:18
【问题描述】:
我想做
String childImg = "childIcon";
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.????, 0, 0, 0);
它需要一个 int 但我有一个字符串。我该如何解决这个问题?
【问题讨论】:
标签: android
我想做
String childImg = "childIcon";
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.????, 0, 0, 0);
它需要一个 int 但我有一个字符串。我该如何解决这个问题?
【问题讨论】:
标签: android
我用它来获取可绘制的 id
getResources().getIdentifier(childImg, "drawable", getPackageName())
【讨论】:
使用getIdentifier()
int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
【讨论】:
这样做:
String childImg = "childIcon";
int id = getResources().getIdentifier(childImg, "drawable", getPackageName())
textView.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
【讨论】:
textView.setCompoundDrawablesWithIntrinsicBounds(Resources.getIdentifier(childImg), 0, 0, 0);
【讨论】: