Android ImageButton 从变量设置ImageResource
我想基于变量以编程方式为 ImageButton 设置 ImageResource 。
例如:如果 size=5,我想将 ImageResource 设置为 R.drawable. Five
if size=6, I want to setImageResource to R.drawable.six
不幸的是,我有太多这样的东西,所以 if-else 或 switch 会很累。
有没有办法实现类似的东西:R.drawable.size?
谢谢 克里斯
I want to setImageResource for an ImageButton programatically, based on a variable.
For eg: if size=5, I want to setImageResource to R.drawable.five
if size=6, I want to setImageResource to R.drawable.six
Unfortunately, I have too many of these, so an if-else or switch gets tiring.
Is there a way to achieve something like: R.drawable.size?
Thanks
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 id 存储在数组中
干杯!
Store the id's in an array
Cheers!
Ya StOle 是对的..使用 int 数组可以解决这个问题。您只需要获取一个增量变量即可访问特定图像
Ya StOle is right.. Using an int array can solve the problem. You just need to get a incrementer variable to access particular image