【问题标题】:android how to get view's ID from name [duplicate]android如何从名称中获取视图的ID [重复]
【发布时间】:2012-12-07 18:18:19
【问题描述】:

可能重复:
How can i get the resource id of an image if I know its name?

您好,我想获取图像的 id,我可以这样做,

int id = R.layout.imagename

那么我可以这样做

imageView.setImageResource(id)

但我的问题是,如果我知道图像名称在运行时发生变化,如何获取 id,那么在这种情况下,我该如何制作 R.layout."imagename"

【问题讨论】:

标签: android


【解决方案1】:

我刚刚复制了Francesco's answer,希望你给他的答案投票

String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    相关资源
    最近更新 更多