android XML 从 XML 获取资源 ID(整数)
我有一个 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<locations>
<location index="0" asset="@drawable/test1"></location>
</locations>
当我解析 XML 并尝试检索 ResourceID 时,它不会返回
int ResourceID = xmlParser.getAttributeIntValue(null, "asset", 0);
XML 中的所有其他值均已成功检索...我做错了什么?
编辑:我想通了,我不想只获取一个 Int
而不是
int ResourceID = xmlParser.getAttributeIntValue(null, "asset", 0);
它
int ResourceID = xmlParser.getAttributeResourceValue(null, "asset", 0);
I have an XML file:
<?xml version="1.0" encoding="UTF-8"?>
<locations>
<location index="0" asset="@drawable/test1"></location>
</locations>
When I parse thru the XML and try to retrieve the ResourceID, it doesn't return
int ResourceID = xmlParser.getAttributeIntValue(null, "asset", 0);
All other values in XML are being retrieved successfully...what am I doing wrong?
EDIT: I figured it out, I'm not wanting to just grab an Int
Instead of
int ResourceID = xmlParser.getAttributeIntValue(null, "asset", 0);
it's
int ResourceID = xmlParser.getAttributeResourceValue(null, "asset", 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上不需要使用 XML 文件来获取可绘制对象的资源 ID。在编译时,会写入“R.java”文件,其中包含资源 id 和实际资源之间的映射。要获取资源 ID,只需使用:
You don't actually need to use the XML file to get the resource ID of your drawable. At compilation the 'R.java' file is written that contains the mappings between resource id's and actual resources . To get the resource Id simply use:
如果您的字符串为“test1”。获取资源 id 的正确方法是
例如。
if you are having the string as 'test1'. The proper way of getting the resource id is
Eg.