Android:定义自定义视图用于内容辅助的属性

时间:2012-08-22 15:01:32

标签: android-layout attributes content-assist graphical-layout-editor

我有一些自定义视图和自定义属性。我想定义我的视图使用哪些属性,以便eclipse可以将其用于内容辅助。

例如,我在res / values文件夹中有一个自定义的xml属性:

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

我的布局文件使用带有自定义属性的自定义视图:

<?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>

如果我使用android:前缀,eclipse会为LinearLayout(TestView派生)提供所有选项,但是如果我使用app:前缀,它会在底部以红色显示“Element com.twp .TestView不可用“。此外,如果我在spColor内部尝试内容辅助,则会显示“内容辅助在当前位置不可用”。我认为既然我定义了“app”命名空间,它就能找到我的样式声明,至少知道spColor是一种颜色。

所以这只是eclipse的限制,还是我可以明确定义视图的自定义属性?

1 个答案:

答案 0 :(得分:1)

我认为declare-styleable中name属性的值需要匹配关联类的非限定名称或关联类的超类的非限定名称。因此,如果TestView的直接超类是LinearLayout,那么关联的declare-styleable中name属性的值应该是TestView,而不是TagAttrs。