textview的背景图片
我有一个文本视图和一个九块图像。在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 setBackgroundResource(),而不是 setBackgroundDrawable。
Use setBackgroundResource(), not setBackgroundDrawable.
我遇到了这个问题,我已经使用了你的答案,但没有用。我使用绘制 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.
使用 setBackgroundResource(R.drawable.image)
Use
setBackgroundResource(R.drawable.image)