【问题标题】:Comments in Android Layout xmlAndroid 布局 xml 中的注释
【发布时间】:2011-03-29 22:01:27
【问题描述】:

我想在布局 XML 文件中输入一些 cmets,我该怎么做?

【问题讨论】:

    标签: android xml


    【解决方案1】:

    正如其他人所说,XML中的注释是这样的

    <!-- this is a comment -->
    

    注意它们可以跨越多行

    <!--
        This is a comment
        on multiple lines
    -->
    

    但它们不能嵌套

    <!-- This <!-- is a comment --> This is not -->
    

    你也不能在标签中使用它们

    <EditText <!--This is not valid--> android:layout_width="fill_parent" />
    

    【讨论】:

    • 另外,您不能在评论中使用双破折号,否则 XML 解析器会报错
    • 如果您使用的是 Eclipse,您可以打开 XML 文件,将光标放在您想要注释的位置,从顶部菜单 Source -> Add Block Comment 中选择。此外,“ctrl + shft + /”(即按住 control 和 shift 键,然后按正斜杠键)。注释代码将在您的光标位于中间的情况下创建,因此您可以开始输入。
    • > 你也不能在标签中使用它们。真的很不幸。
    【解决方案2】:

    万维网联盟 (W3C) 实际上定义了一个评论界面。定义是all the characters between the starting ' &lt;!--' and ending '--&gt;' form a part of comment content and no lexical check is done on the content of a comment

    更多详情请访问developer.android.com 网站。

    因此,您可以简单地在任何开始和结束标记之间添加您的评论。在 Eclipse IDE 中,只需键入 &lt;!-- 即可自动为您完成评论。然后,您可以在两者之间添加您的评论文本。

    例如:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        tools:context=".TicTacToe" >
    
     <!-- This is a comment -->
    
    </LinearLayout>
    

    特别提到in between 的目的是因为你不能在标签中使用它。

    例如:

    <TextView 
        android:text="@string/game_title"
        <!-- This is a comment -->
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
    

    错了会报如下错误

     Element type "TextView" must be followed by either attribute specifications, ">" or "/>".
    

    【讨论】:

    • 注意:标签内没有 cmets。这应该是选择的答案
    • 向 Android Studio 团队提交了一项改进。如果我使用数据绑定并想在 XML 中注释我编写了一些数据绑定逻辑的行,我必须在其他地方进行注释,这对可见性或注释所指的部分没有帮助。这不是不可能做到的事情,应该让我们(开发者)使用。
    【解决方案3】:

    XML cmets 以&lt;!-- 开头并以--&gt; 结尾。

    例如:

    <!-- This is a comment. -->
    

    【讨论】:

      【解决方案4】:

      有两种方法可以做到这一点

      1. "&lt;!--"开始您的评论,然后以“--&gt;"”结束您的评论

        例如&lt;!-- my comment goes here --&gt;

      2. 突出显示您要评论的部分并按CTRL + SHIFT + /

      【讨论】:

        【解决方案5】:

        ctrl+shift+/可以对代码进行注释。

        <!--    
             <View
                  android:layout_marginTop="@dimen/d10dp"
                  android:id="@+id/view1"
                  android:layout_below="@+id/tv_change_password"
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="#c0c0c0"/>-->
        

        【讨论】:

          【解决方案6】:
          <!-- comment here -->
          

          【讨论】:

            【解决方案7】:

            可以在标签内评论

            可以创建可用于评论/文档目的的自定义属性。

            在下面的示例中,定义了一个documentation:info 属性,并带有一个示例注释值:

            <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:documentation="documentation.mycompany.com"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/relLayoutID"
                documentation:info="This is an example comment" >
            
                <TextView
                    documentation:purpose="Instructions label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Click here to begin."
                    android:id="@+id/tvMyLabel"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    documentation:info="Another example comment"
                    documentation:translation_notes="This control should use the fewest characters possible, as space is limited"
                    />
            
            </RelativeLayout>
            

            请注意,在这种情况下,documentation.mycompany.com 只是新自定义 XML 命名空间(documentation)的定义,因此只是 a unique URI string - 只要它是唯一的,它可以是任何东西。 documentation 右侧的 documentation 也可以是任何东西 - 这与定义和使用 android: XML 命名空间的方式相同。

            使用此格式,可以创建任意数量的属性,例如documentation:infodocumentation:translation_notes 等,以及描述值,格式与任何 XML 属性相同。

            总结:

            • xmls:my_new_namespace 属性添加到XML 布局文件中的根(顶级)XML 元素。将其值设置为唯一的字符串
            • 在文件中的任何子 XML 元素下,使用新的命名空间和后面的任何单词来定义编译时忽略的注释标记,例如&lt;TextView my_new_namespace:my_new_doc_property="description" /&gt;

            【讨论】:

            • 请注意,这些属性在构建过程中不会被丢弃,而是存储在生成的 APK 中。考虑改用特殊的 tools: 命名空间,它确实会被丢弃。 (发布此答案时它可能不存在,但此页面继续获得新的查看者。)
            • @j__m 这是一个很好的观点。我还没有研究过 ProGuard 是否可以自动删除它,或者通过一些配置......
            【解决方案8】:

            如果您想在Android Studio 中发表评论,只需按:

            Ctrl + / 在 Windows/Linux 上

            Cmd + / 在 Mac 上。

            这适用于strings.xml 等XML 文件以及MainActivity.java 等代码文件。

            【讨论】:

              【解决方案9】:

              点击

              ctrl+shift+/

              写下你和所有东西都会在 cmets 中的任何东西

              【讨论】:

                【解决方案10】:

                您也可以通过按 Ctrl+shift+/ 和 shift+/ 来添加注释。

                【讨论】:

                  【解决方案11】:

                  来自 Federico Culloca 的笔记:

                  你也不能在标签内使用它们

                  意味着;您必须将注释放在文件的顶部或底部 - 您真正想要添加 cmets 的所有位置至少都在顶级布局标签内

                  【讨论】:

                  • 不是这个意思。您可以完美地在文件中间的某处放置注释。它只需要在其他标签之间。
                  • 更具体地说,它们必须按以下顺序排列:元素n的结束标记、注释、元素n+1的开始标记。
                  【解决方案12】:

                  难以置信的是,2019年用Android studio 3.3(我不知道确切的版本,至少3.3),可以对xml使用双斜杠注释。

                  但是如果你在 xml 中使用双斜杠注释,IDE 会显示警告。

                  <?xml version="1.0" encoding="utf-8"?>
                  <android.support.constraint.ConstraintLayout 
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:app="http://schemas.android.com/apk/res-auto"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
                  
                      // this works
                  
                      /* this works too */
                  
                      /*
                      multi line comment
                      multi line comment
                      */
                  
                      <TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Hello World! yeah"
                          app:layout_constraintBottom_toBottomOf="parent"
                          app:layout_constraintLeft_toLeftOf="parent"
                          app:layout_constraintRight_toRightOf="parent"
                          app:layout_constraintTop_toTopOf="parent" />
                  
                  </android.support.constraint.ConstraintLayout>
                  

                  【讨论】:

                  • 它会显示警告Unexpected text found in layout file: ...
                  猜你喜欢
                  • 2011-12-09
                  • 2013-03-26
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-11-21
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多