سؤال

I've got some custom views and custom defined attributes. I'd like to define which attributes my views use so that eclipse can use that for content-assist.

For example, I've got a custom xml attribute declared like so in my res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TagAttrs">
        <attr name="spColor" format="color" />
    </declare-styleable>
</resources> 

And my layout file uses a custom view with the custom attribute:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.twp"
    ...
    >

    <com.twp.TestView
        android:id="@+id/testView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:spColor="#FF00FF" >
       ...
   </com.twp.TestView>
</LinearLayout>

If I use the android: prefix, eclipse presents all the options it would for a LinearLayout (from which TestView derives), but if I use the app: prefix, it shows in red at the bottom that "Element com.twp.TestView not available". Also, if I try content-assist inside of spColor, it says "Content Assist not available at the current location." I would think since I defined the "app" namespace, it would be able to find my stylable declaration and at least know that spColor is a color.

So it this just a limitation of eclipse, or can I explicitly define a view's custom attributes?

هل كانت مفيدة؟

المحلول

I believe the value for the name attribute in declare-styleable needs to match the unqualified name of the associated class or the unqualified name of a superclass of the associated class. So if the direct superclass of TestView is LinearLayout, then the value of the name attribute in the associated declare-styleable should be TestView, not TagAttrs.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top