使用数组引用作为自定义 Android 视图的 XML 属性
这个问题已经解决了,详情见评论。
我正在扩展现有的 Android 视图并加载一些自定义属性,如 中所述使用 XML 声明自定义 Android UI 元素 和定义自定义属性。
具有布尔值和整数格式的属性工作正常,但是当我尝试指定对数组资源的引用时,应用程序在启动时崩溃。我在 xml 资源文件中定义了一个整数数组,并尝试将其用作自定义视图的属性。
我可以使用数组资源来设置 android Spinner 类的“entries”属性,没有错误,所以这似乎是我的实现中的问题。 logcat 消息似乎没有提供有关崩溃的任何具体信息,但我仍在寻找,因此如果我发现某些内容,我会更新。
属性由(在 attrs.xml 中)声明:
<declare-styleable name="CustomView">
<attr name="values" format="reference"/>
<attr name="isActive" format="boolean"/>
</declare-styleable>
数组定义为(在 arrays.xml 中):
<integer-array name="nums">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
我通过以下方式引用该数组:
<com.test.CustomView cv:values="@array/nums" />
这会导致应用程序立即崩溃。此外,如果我引用颜色资源而不是数组,那么应用程序不会崩溃。有人知道如何处理这个问题吗?
This problem has been solved, see comments for details.
I am extending an existing Android View and loading some custom attributes, as described in Declaring a custom android UI element using XML and Defining custom attrs.
Attributes with boolean and integer formats work fine, but when I try to specify a reference to an array resource, the application crashes at launch. I have defined an integer array inside an xml resource file and I'm trying to use it as an attribute for the custom view.
I can use the array resource to set the "entries" attribute of the android Spinner class with no errors, so it seems to be a problem in my implementation. The logcat messages don't seem to supply any specific information about the crash, but I'm still looking so I will update if I find something.
The attributes are declared by (in attrs.xml):
<declare-styleable name="CustomView">
<attr name="values" format="reference"/>
<attr name="isActive" format="boolean"/>
</declare-styleable>
The array is defined as (in arrays.xml):
<integer-array name="nums">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
And I am referencing the array by:
<com.test.CustomView cv:values="@array/nums" />
And this causes the application to crash immediately. In addition, if I reference a color resource instead of an array then the application does not crash. Does anybody know how to deal with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里只是附带你的问题,因为如果你在谷歌上搜索“数组引用 XML 属性自定义视图”之类的内容,你的帖子会首先出现,所以有人可能会发现这很有帮助。
如果你希望你的自定义视图引用字符串数组,您可以使用 Android 现有的
android:entries
XML 属性,而不是创建全新的自定义属性。只需在
res/values/attrs.xml
中执行以下操作:然后在自定义 View 的构造函数中执行此操作:
然后您应该能够通过
android:entries
引用字符串数组当您将自定义视图添加到 XML 布局文件时,使用 code> 属性。示例:这正是 ListView 类中的完成方式(查看源代码,你就会看到)。
Just going to piggyback off your question here, since your post shows up first if you google something like "array reference XML attribute custom view", so someone might find this helpful.
If you want your custom view to reference an array of strings, you can use Android's existing
android:entries
XML attribute, instead of creating a totally new custom attribute.Just do the following in
res/values/attrs.xml
:Then do this in your custom View's constructor:
And then you should be able to reference a string array via the
android:entries
attribute when you add your custom View to an XML layout file. Example:This is exactly how it is done in the ListView class (look in the source, you'll see).
另一个答案适用于字符串数组。但是,引用数组上的
arr.getTextArray(...)
将为您提供
res/drawable/a.png
作为 CharSequence而不是资源 ID。解析引用数组的正确方法是这样的:
The other answer works well with array of strings. However,
arr.getTextArray(...)
on array of references, e.g.will give you
res/drawable/a.png
as the CharSequence instead of the resource id.The proper way to parse an array of references is this:
问题是关于获取一个整数数组,对于我的情况,我需要从数组中读取自定义视图的颜色(int),可定义如下:
然后我使用我的自定义视图,如下所示:
颜色定义如下:
所以我需要获取自定义视图(ColorPickerView)中的颜色,代码如下:
这是颜色数组的打印:
希望这会对一些人有所帮助。
The question is about obtain an integer array, for my case, I need to read the colors(int) from an array for my custom view, styeable definition as below:
Then I use my custom view like below:
The colors definition is as below:
So I need to obtain the colors in my custom view (ColorPickerView), code as below:
Here is the print of colorsArray:
Hope this will help some guys.