Android ImageButton 从变量设置ImageResource

发布于 2024-09-09 07:13:04 字数 297 浏览 1 评论 0原文

我想基于变量以编程方式为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-09-16 07:13:04

将 id 存储在数组中

final int[] imgSizeIds = new int[]{ R.drawable.zero,R.drawable.one,R.drawable.two, .... };

then, 
setImageResource(imgSizeIds [ size ] );

干杯!

Store the id's in an array

final int[] imgSizeIds = new int[]{ R.drawable.zero,R.drawable.one,R.drawable.two, .... };

then, 
setImageResource(imgSizeIds [ size ] );

Cheers!

奢望 2024-09-16 07:13:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文