TypedArray 和 styleable 属性:getIndexCount() 与 length()
我正在解析自定义属性,并且遇到了一些奇怪的事情。假设我的解析器看起来像这样:
final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.Item);
final int size = attributes.getIndexCount();
for(int i = 0; i < size; i++) {
final int attr = attributes.getIndex(i);
if(attr == R.styleable.Item_custom_attrib) {
final int resourceId = attributes.getResourceId(attr, -1);
if(resourceId == -1)
throw new Resources.NotFoundException("The resource specified was not found.");
...
}
attributes.recycle();
这有效。现在,如果我用 final int size = attribute.length();
替换第 2 行,这意味着我得到:
final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.Item);
final int size = attributes.length();
for(int i = 0; i < size; i++) {
final int attr = attributes.getIndex(i);
if(attr == R.styleable.Item_animation_src) {
final int resourceId = attributes.getResourceId(attr, -1);
if(resourceId == -1)
throw new Resources.NotFoundException("The resource specified was not found.");
...
}
attributes.recycle();
这会因我抛出的 Resources.NotFoundException 而崩溃。换句话说,attributes.getResourceId(attr, -1);
返回默认的-1
值。
现在,在这种特殊情况下,只有一个自定义属性。 attributes.getIndexCount()
和 attributes.length()
都返回 1,因为我的属性中确实有一个值。这意味着 getIndex(i) 应该返回相同的数字,但事实并非如此。这意味着 getIndexCount()
不仅仅只是返回数组中包含数据的索引数量
。这两种方法之间到底有什么区别,一种方法允许我获取属性,而另一种方法不允许我获取属性?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是在摆弄这个,我发现整个事情令人困惑,但我想我已经弄清楚了:
所以你已经在 attrs.xml 中为你的类声明了 N 个属性。
现在获得的
TypedArray
的大小是N。它总是传递相同大小的数组。但也许您在 layout xml 中只定义了 X 个属性,因此
getIndexCount()
返回 X。在您的小部件的 init 中,如果您要问自己有多少个属性以及哪些属性
如果在 xml 中定义,您首先需要使用
getIndexCount()
找出有多少个。然后从
0 循环到 getIndexCount()-1
并询问TypedArray
:“第 i 个提供的属性?”。
所以假设您想强制在 xml 中设置一个属性,您现在可以检查这个
因为对 getIndex() 的调用之一必须返回您正在查找的 id。
但您始终可以忽略这一点并提供默认值。然后你可以调用 a.getInteger( R.styleable.MyClass_MyAttribute, defaultValue );
你可以检查 R.java 以查看每个类的所有属性 ID 都从 0 开始。生成的注释也有助于理解。
I was just fiddling with this and I found the whole thing confusing, but I think I got it figured out:
So you have declared N attributes in attrs.xml, for your class.
Now the size of the obtained
TypedArray
is N. It always passes the same sized array.But maybe you defined only X attributes in your layout xml, so
getIndexCount()
returns X.In your widget's init, if you were to ask yourself how many and which attributes
were defined in xml, you would first find out how many, using
getIndexCount()
.Then you loop from
0 to getIndexCount()-1
and ask theTypedArray
: "what's the index ofthe i-th supplied attribute?".
So suppose you want to force an attribute to be set in xml, you can now check this
because one of the calls to getIndex() has to return the id you are looking for.
But you can always ignore that, and supply default values. Then you can just call
a.getInteger( R.styleable.MyClass_MyAttribute, defaultValue );
You can check R.java to see that all attribute IDs start at 0, for every class. The generated comments are also helpful to understand.
你的问题确实很简单,在常用中,就像这样,首先定义一个类myview extends View{...},然后将myview应用到layout.xml并定义一些属性,如'textColor =“#ffffffff”'等,在layout.xml,所以你的 a.getIndexCount() 返回不为空或不为零,这就是你所期望的。
现在我改变问题,你定义一个类myview extends Object,注意不是extends View类,你永远不会使用layout.xml到你自定义的myview。整个事情就像
android定义的类View实现了Drawable.Callback、Drawable.Callback2、KeyEvent.Callback、AccessibilityEventSource{...},没有layout.xml,没有extends View方便,
您唯一可以使用的资源是 attrs.xml、styles.xml。
那么你可以做什么让 a.getIndexCount() 返回不为空或不为零
像mytop:为什么TypedArray.getIndexCount()总是返回0
you question is very simple indeed in common use,like this,first defines a class myview extends View{...},and then apply myview to layout.xml and define some attributes like 'textColor="#ffffffff"' etc,in the layout.xml,so your a.getIndexCount() return not null or not zero,that is what you expected.
now i change the question, you define a class myview extends Object,note not extends View class,and you will never use the layout.xml to your self-defined myview.the whole things like
the android defined class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource{...},no layout.xml,no extends View convenience,the
only resources you can use is attrs.xml,styles.xml.
so what you able to do to let a.getIndexCount() return not null or not zero
like mytop: why TypedArray.getIndexCount() always returns 0