如何动态改变图像按钮的Image?

发布于 2024-11-17 09:04:26 字数 127 浏览 3 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(5

靑春怀旧 2024-11-24 09:04:26

请将图像指定为“背景”图像,而不是 ImageButton 中的“src”。

例如:-

<ImageButton android:id="@+id/img1" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:background="@drawable/btn_yes" />

在编码端动态更新如下。

img1.setBackgroundResource(R.drawable.btn_no);

Please assign image as "background" image instead of "src" in ImageButton.

Ex:-

<ImageButton android:id="@+id/img1" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:background="@drawable/btn_yes" />

And In codding side dynamically update as below.

img1.setBackgroundResource(R.drawable.btn_no);
悲念泪 2024-11-24 09:04:26

您可以使用 invalidate() 或 postInvalidate() 取决于您是否在 UI 线程中。

You can use invalidate() or postInvalidate() depends whether or not you are in UI thread.

风月客 2024-11-24 09:04:26

无需刷新。从我的角度来看,就像分页一样,如果您单击下一个按钮来进行该按钮的事件处理,只需替换您想要放置的可绘制资源。我从你的要求中了解到的。你能说得更具体一些吗?

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?

沉默的熊 2024-11-24 09:04:26

正如几个人提到的,使用 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.

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