2

我想要做

String childImg = "childIcon";
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.????, 0, 0, 0);

它期待一个 int 但我有一个字符串。我该如何解决这个问题?

4

4 回答 4

3

我用它来获取可绘制的 id

getResources().getIdentifier(childImg, "drawable", getPackageName())
于 2012-05-31T21:09:37.730 回答
1

采用getIdentifier()

int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
于 2012-05-31T20:44:45.797 回答
1

像这样做:

String childImg = "childIcon";
int id = getResources().getIdentifier(childImg, "drawable", getPackageName())
textView.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
于 2012-05-31T20:46:36.953 回答
0
textView.setCompoundDrawablesWithIntrinsicBounds(Resources.getIdentifier(childImg), 0, 0, 0);
于 2012-05-31T20:44:28.280 回答