【问题标题】:How to add documentation to custom attributes?如何将文档添加到自定义属性?
【发布时间】:2012-07-18 22:37:18
【问题描述】:

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

<?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=,它将为您提供以下信息,“指定视图的基本宽度。[维度,枚举]”

有没有办法为你自己的 attrs 提供这些?

谢谢

【问题讨论】:

  • 你找到答案了吗?
  • @drewhannay 我放弃了寻找,还没有找到任何东西。
  • 2018 仍然没有标准解决方案

标签: android xml android-xml attr


【解决方案1】:

为每个元素添加 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}

【讨论】:

  • 就是这么简单!我怎么没想到呢?
【解决方案2】:

您需要在代码中编写构造函数,然后将它们链接到 xml。阅读this了解更多信息。

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多