如何动态改变图像按钮的Image?
我在 ImageButtons 上显示了两组图像,并且我通过动态设置它们来更改图像。 但新的/更改的图像不会反映在屏幕上。我相信这与设置图像后刷新屏幕有关。 谁能帮助我如何动态更改和显示图像按钮上的图像?
- 提前致谢
I have two set of Images being displayed on ImageButtons and I am changing the images by setting them dynamically.
but the new/ changed image is not reflected on screen. I believe it has something to do with refreshing the screen after setting the image.
can anyone help me how the image on a imagebutton can be changed dynamically and displayed?
--Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请将图像指定为“背景”图像,而不是 ImageButton 中的“src”。
例如:-
在编码端动态更新如下。
Please assign image as "background" image instead of "src" in ImageButton.
Ex:-
And In codding side dynamically update as below.
这里: http://developer.android.com/reference/android/view /View.html#invalidate()
Here: http://developer.android.com/reference/android/view/View.html#invalidate()
您可以使用 invalidate() 或 postInvalidate() 取决于您是否在 UI 线程中。
You can use invalidate() or postInvalidate() depends whether or not you are in UI thread.
无需刷新。从我的角度来看,就像分页一样,如果您单击下一个按钮来进行该按钮的事件处理,只需替换您想要放置的可绘制资源。我从你的要求中了解到的。你能说得更具体一些吗?
No need to refresh. From my point of view, like pagination, if you click next button for that button's event handling just replace the drawable resource what you want to put. What I understand from your requirement. Can you be more specific?
正如几个人提到的,使用 validate 或 postvalidate 在所有情况下都适合我。
然而,听起来 Android 的 LevelListDrawable 资源似乎是更适合您的选项 - 这允许您使用 imageview 上的简单 SetImagelevel 调用从图像列表中选择要显示的图像。
As mentioned by several people using validate or postvalidate works for me in all contexts.
However it sounds as if Android's LevelListDrawable resource is a more appropriate option for you - this allows you to select which image is being displayed from a list of images using a simple SetImagelevel call on the imageview.