如何为自定义属性添加文档?

21

我知道如何为视图创建自定义属性,例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SomeViewsCustomAttrs">
        <attr name="someAttr" format="dimension" />
        <attr name="anotherAttr" format="boolean" />
    </declare-styleable>
</resources>    

我想知道是否有一种方法可以为这些自定义属性添加文档。在XML编辑器中编辑布局时,您可以获取描述属性的工具提示。例如,如果您正在输入android:layout_width=,它将提供以下信息:“指定视图的基本宽度。[dimension,枚举]”。

是否有办法为自己的属性提供类似的文档?

谢谢


1
你有没有找到这个问题的答案? - drewhannay
@drewhannay我放弃了寻找,还没有找到任何东西。 - cottonBallPaws
2018年了,仍然没有标准解决方案。 - Kedar Paranjape
2个回答

6

为每个元素添加XML注释:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SomeViewsCustomAttrs">
        <!-- Default highlight color for items that are pressed. -->
        <attr name="colorPressedHighlight" format="color" />
        <!-- Default highlight color for items that are long-pressed. -->
        <attr name="colorLongPressedHighlight" format="color" />
    </declare-styleable>
</resources> 

在您视图的Java源文件中的Java Doc中,可以像这样链接到属性(来自TextView的示例):

* See {@link android.R.styleable#TextView TextView Attributes}, {@link android.R.styleable#View View Attributes}

那太容易了!我为什么没想到呢? - SMBiggs

-1
你需要在代码中编写构造函数,然后将它们链接到XML。阅读 this 获取更多信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接