Android自定义视图获取多个属性 [英] Android custom view obtain multiple attributes

查看:62
本文介绍了Android自定义视图获取多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android中创建自定义视图.在它的构造函数中,我获得了XML布局文件中设置的一些属性.代码是这样的:

I am creating a custom view in Android. In it's constructor I am obtaining some of the attributes that are set in the XML layout file. The code is like this:

public LabeledEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

    TypedArray styledAttrs = context.getTheme().obtainStyledAttributes(attrs, new int[] { android.R.attr.id, android.R.attr.digits, android.R.attr.padding, android.R.attr.inputType }, 0, 0);
    try {
        int id = styledAttrs.getResourceId(styledAttrs.getIndex(0), -1);
        String digits = styledAttrs.getString(styledAttrs.getIndex(1));
        float padding = styledAttrs.getDimension(styledAttrs.getIndex(2), 0.1f);
        int inputType = styledAttrs.getInt(styledAttrs.getIndex(3), -1);
    } finally {
        styledAttrs.recycle();
    }
}

问题在于即使这些属性存在于属性集中,其均不会获得所有属性.甚至更奇怪的是,如果我更改int数组中id的顺序,则会得到不同的结果.例如,如果我使用以下顺序

The problem is that obtainStyledAttributes does not obtain all the attributes, even though they exist in the attribute set. What's even stranger, is that if I change the ordering of the ids in the int array I get different results. For example, if I use the following ordering

new int[] { android.R.attr.id, android.R.attr.digits, android.R.attr.padding, android.R.attr.inputType }

我获得了3个属性,但是如果我使用以下顺序

I get back 3 attributes, but if I use the following ordering

new int[] {android.R.attr.digits, android.R.attr.padding, android.R.attr.inputType, android.R.attr.id }

我回来了.2.我包括了这2个案例的手表窗口的2个屏幕截图.断点是在try语句之后设置的.

I get back 2. I'm including 2 screenshots of the watches window of these 2 cases. The breakpoint is set just after the try statement.

无论如何,如果我一次获得一个属性,则对所有这些属性都有效.getsStyledAttributes如何工作?另外,我不确定是否应该使用styledAttrs.getIndex(i)函数,但这在解决当前函数之后是一个问题.

In any case if I obtain the attributes one at a time, it works for all of them. How does obtainStyledAttributes works? Also I'm not sure if I should use the styledAttrs.getIndex(i) function or not, but that is a problem after the current one is solved.

推荐答案

尽管未记录,但 obtainStyledAttributes 需要一个排序后的数组,并且在此假设下对其代码进行了优化.

Although it is not documented, obtainStyledAttributes expects a sorted array, and its code is optimized with that assumption.

因此,您需要为 styledAttrs 数组提供根据其id值按升序排列的元素.

So you need to provide your styledAttrs array with the elements sorted in ascending order according to their id values.

有几种方法可以确定正确的顺序:

There are several ways you can determine the correct order:

  • 基于它们在资源中的相对位置
  • 通过检查它们的相对值并更改数组以使其匹配
  • 或通过编程方式对数组元素进行排序

如果您选择在运行时以编程方式对数组进行排序,请确保在调用 getString()等时使用适当的(排序的)索引.

If you choose to sort the array programmatically at run time, make sure you use the appropriate (sorted) index when calling getString() etc.

另一个常见的解决方法是一次仅使用 obtainStyledAttributes 获得单个值.(具有一个元素的数组已经排序.)

Another common workaround is to only get a single value with obtainStyledAttributes at a time. (An array with one element is already sorted.)

我也不知道是否应该使用styledAttrs.getIndex(i)函数

Also I'm not sure if I should use the styledAttrs.getIndex(i) function or not

我相信只有在遍历 TypedArray 且可能其中某些元素可能没有值的情况下才有必要.它基本上为您隐藏"了null元素,就好像它是一个稀疏数组一样.通常,除非您以某些方式(例如在实现自定义视图时)访问样式化资源,否则我认为没有必要.

I believe that's only necessary if you're looping through the TypedArray and its possible some of the elements may not have values; it basically "hides" the null elements for you as though it were a sparse array. In general I don't think it's necessary unless you're accessing styled resources in certain ways, for example when implementing a custom view.

大多数时候,我使用与视图完全不相关的自定义主题属性,在这种情况下,我总是发现 TypedArray.getIndex()是多余的.

Most of the time I use custom theme attributes that aren't related to a view at all, and in those cases I've always found TypedArray.getIndex() to be redundant.

这篇关于Android自定义视图获取多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆