android - 在自定义视图中从 Android attr 获取字符串值的资源 ID (R.string.name)
问题描述
attrs.xml
我的应用程序有一个自定义视图,按条目设置样式:
<resources>
<declare-styleable name="MyView">
<attr name="title" format="reference" />
<attr name="image" format="reference" />
</declare-styleable>
</resources>
在我的自定义视图中,我需要检索指定为title
in的资源的资源 ID attrs.xml
,但我只能检索指定为 的字符串的内容title
。到目前为止,我尝试过使用:
attrs.getAttributeResourceValue(R.styleable.MyView_title, 0)
和
getSourceResourceId(R.styleable.MyView_title, 0)
但在这两种情况下,返回的值都是 default 0
。
我对其他类型的属性尝试了相同的方法,例如,Drawable ID 可以通过以下方式轻松检索:
getResourceId(R.styleable.MyView_image, 0)
有什么方法可以检查 XML 中提供了什么 R.string 吗?
解决方案
推荐阅读
- javascript - 如何用 HOC 包装每个导出的组件?
- python - 在 Python 中导入继承的类
- minizinc - Minizinc:餐桌上的最佳排序功能
- python - 在 Python 中为 Tkinter 的 Combobox 中的 Excel 更新列名
- javascript - 日期/时间消息的 JavaScript 递归函数
- html - 如何将 Flexbox 子背景设置为 100% 高度并垂直居中文本
- ios - 更新结构实例
- exiftool - 使用未定义的标签添加到 exifdata
- error-handling - 如何在 sapper (svelte) 中捕获服务器错误
- php - PHP MySQL 覆盖 SELECT 语句返回 null