I can't get styleable array of attrs

2019-07-07 06:05发布

问题:

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.

回答1:

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

File -> Invalidate Caches / Restart