textview的背景图片

发布于 2024-10-10 15:21:59 字数 461 浏览 9 评论 0原文

我有一个文本视图和一个九块图像。在 xml 编辑器中将此图像分配给“背景”标签即可按需要进行操作。现在我想在编码中动态设置这个图像,我认为

myTextView.setBackgroundDrawable(mydrawable);

这是正确的做法。我的 png 位于 res/drawable 中。 API 规定 mydrawable 需要是一个类型

 R.drawable.myninepatch

为int 的drawable 对象。所以我的问题是我必须以某种方式从该 int 转换为可绘制对象。然后我想我可以使用构造函数

NinePatchDrawable mydrawable = new NinePatchDrawable();

但我再次无法构造这样的对象。有人有想法吗?

谢谢。

I have a textview and a nine-patch image. Assigning this image in the xml editor to the tag "background" works as desired. Now I want to set this image dynamically in the coding, I thought

myTextView.setBackgroundDrawable(mydrawable);

would be the right thing to do. My png is in res/drawable. The API says that mydrawable needs to be an object of type drawable

 R.drawable.myninepatch

is of type int. So my problem is that I have to convert from that int to a drawable somehow. Then I thought I can use a constructor

NinePatchDrawable mydrawable = new NinePatchDrawable();

But I am again not able to construct such an object. Anyone an idea?

Thanks.

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

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

发布评论

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

评论(3

荒路情人 2024-10-17 15:21:59

使用 setBackgroundResource(),而不是 setBackgroundDrawable。

Use setBackgroundResource(), not setBackgroundDrawable.

忱杏 2024-10-17 15:21:59

我遇到了这个问题,我已经使用了你的答案,但没有用。我使用绘制 9 补丁填充像素解决了这个问题。

I had the problem, and I've used your answer but didn't work. I solved it using the draw 9 patch padding pixels.

故事与诗 2024-10-17 15:21:59

使用 setBackgroundResource(R.drawable.image)

Use setBackgroundResource(R.drawable.image)

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