本文介绍了我无法获得可样式化的attrs数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要声明一个具有attrs的可样式化视图,并通过以下简单方法创建了文件myview_attrs.xml:

<resources>
    <declare-styleable name="TestStyleable">
        <attr name="testAttr" format="integer"/>
    </declare-styleable>
</resources>

然后,我想在我的代码中加载此attr:

void loadAttributes(AttributeSet attrs) {
    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.);
}

R.styleable.之后,使用ctrl-SPACE,这些是我得到的选项:

那显然是错误的.确实,我不能选择 R.styleable.TestStyleable ,因为菜单中未建议使用 R.styleable.TestStyleable_testAttr ,但这没有任何意义./p>

当然,如果我选择第二个,它会给我错误,因为期望使用 int [] ,而我放置了 int .另一方面,如果我手动编写 R.styleable.TestStyleable 会失败,并显示无法解析符号".

我在这里想念什么?

更新:这最终是一个Android Studio错误.

解决方案

正如@Yurii Tsap所说,这是一个Android Studio错误,最终通过修复得到了解决

文件->使缓存无效/重新启动

I am declaring a styleable view with attrs, and I created the file myview_attrs.xml this simple way:

<resources>
    <declare-styleable name="TestStyleable">
        <attr name="testAttr" format="integer"/>
    </declare-styleable>
</resources>

Then, I want to load this attr in my code:

void loadAttributes(AttributeSet attrs) {
    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.);
}

After R.styleable. , with ctrl-SPACE, these are the options I am given:

That is obviously wrong. Indeed, I can't select R.styleable.TestStyleable, because it's not proposed in the menu, but only R.styleable.TestStyleable_testAttr, that does not make sense.

Of course, if I choose the second one it gives me error, because a int[] is expected and I put an int. On the other side, if I manually write R.styleable.TestStyleable fails with "Cannot resolve symbol".

What am I missing here??

Update: this ended to be an Android Studio bug.

解决方案

As @Yurii Tsap stated, this is an Android Studio bug, and it finally got fixed by making

File -> Invalidate Caches / Restart

这篇关于我无法获得可样式化的attrs数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 05:18