使用 Android 在 xml 中使用 ShapeDrawable 绘制多个形状
我目前正在代码中的自定义视图中在画布上绘制多个圆圈。圆圈是静态的并且不会改变。我想使用 xml 中的 ShapeDrawable 来绘制它们,以帮助清理我的代码。我将有许多不同的可绘制对象供用户选择,因此我不想在代码中执行此操作。对我来说,拥有 3 或 4 个 xml 可绘制对象似乎更整洁。
我使用 ShapeDrawable 在 xml 中创建了一个圆圈,但无法向 xml 添加多个形状。
如何使用 ShapeDrawable 将多个形状添加到 xml 文档。
I am currently drawing a number of circles on a canvas in a custom view in code. the circles are static and do not change. I would like to draw them using a ShapeDrawable in xml to help clean up my code. I will have a number of different drawables which the user can select and therefore I don't want to do this in code. having 3 or 4 xml drawables seems a lot neater to me.
I have created one circle in xml using a ShapeDrawable but am unable to add more than one shape to the xml.
How do I add multiple shapes to an xml document using ShapeDrawable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是我如何制作一个带有单像素黑色边框的红色填充圆圈,中间有一个白色数字 72:
在 res\drawable 中创建一个 XML 文件并适当命名它,例如 red_circle_black_border.xml
然后,在您的布局中使用它,声明如下:
显然,根据需要更改textSize和layout_width/height:-)
Here's how I did a filled red circle with a one-pixel black border, with a white number 72 in the middle:
Create an XML file in res\drawable and name it appropriately, eg red_circle_black_border.xml
Then, to use it in your layout, declare it as follows:
Obviously, change the textSize and layout_width/height as required :-)
我想我已经找到了使用图层列表将多个形状添加到我的 xml 文件中的解决方案。
我还没有尝试过这个,但这似乎不正是我想要的,因为形状将按照其数组索引的顺序绘制。
博客文章
Android 文档
I think I have found a solution to add multiple shapes to my xml file using Layer-list.
I havent tried this yet but this seems t be exactly what I want as the shapes will be draw in the order of their array index.
Blog post
Android docs
我有一个带有 2 个口腔超大号的复选标记图标示例可能会对您有所帮助,请将其放入可绘制文件夹中
I have an example check mark icon with 2 oral outsize may help you, put this in drawable folder