【问题标题】:Android: define which attributes a custom view uses for content assistAndroid:定义自定义视图用于内容辅助的属性
【发布时间】:2012-08-22 15:01:31
【问题描述】:

我有一些自定义视图和自定义属性。我想定义我的视图使用哪些属性,以便 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 的限制,还是我可以显式定义视图的自定义属性?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      相关资源
      最近更新 更多