如何通过代码设置drawableRight资源?

发布于 2024-12-03 19:04:29 字数 96 浏览 2 评论 0原文

我试图在单击按钮后在按钮的右侧设置图像。我想通过代码来做到这一点。

我已经了解了如何通过代码更改背景资源,但我找不到任何示例来说明如何通过代码更改侧面。是否可以?

I am trying to set an image on the right side of my button after the button has been clicked. I want to do this via code.

I have seen how to change the background resource via code but I am not able to find any examples showing how to change the sides via code. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

素染倾城色 2024-12-10 19:04:29

您需要使用带有 null 的方法。

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, 
 Drawable bottom)

对于任何不需要的内容,

You need to use the

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, 
 Drawable bottom)

method with null for any that are not needed.

我们的影子 2024-12-10 19:04:29

通常您可以使用此更改

Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);

请注意您可能会错过按钮文本。

Usually you can change using this

Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);

Be aware you can miss the button text.

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