问题 布局XML中的注释不起作用


是的,我查了一下!

<!-- comment --> 

似乎是正确的选择, 但我在android-studio中收到错误 Gradle:解析XML时出错:格式不正确(无效令牌)

当我这样做的时候

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    <!-- android:background="?android:attr/selectableItemBackground" -->
    android:id="@+id/imageButton" 
    android:layout_alignParentTop="true"
    android:src="@drawable/gfx_select_medium"
    android:layout_marginRight="22dp"/>

任何帮助表示赞赏,谢谢!


2899
2017-11-02 15:59


起源

将它移到ImageButton之外并尝试 - Raghunandan
请发布整个XML,可能错误在您的代码而不是注释中。 - nstosic
@NitroNbg相信我。你不能在标签xml中放置注释 - Blackbelt


答案:


XML注释不能放在标记标记内。移动评论例如在您的上方或下方 ImageButton 标签。

这是 规范 参考,重点补充:

评论可能出现在文档的任何位置  其他 标记

哪里 标记 定义为:

标记采用开始标记,结束标记,空元素标记,实体引用,字符引用,注释,CDATA部分分隔符,文档类型声明,处理指令,XML声明,文本声明以及任何处于的空白区域的形式。文档实体的顶级(即文档元素之外,而不是任何其他标记内)


9
2017-11-02 16:01



是的,但为什么不可能呢? - Blackbelt


它不仅仅是AS。您需要将注释放在结束标记之外 </> 做一些类似的事情

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton" 
android:layout_alignParentTop="true"
android:src="@drawable/gfx_select_medium"
android:layout_marginRight="22dp"/>    
<!-- android:background="?android:attr/selectableItemBackground" -->

看到 这个链接 和 这个 从W3关于xml评论。简而言之,它说

[定义:注释可能出现在其他标记之外的文档中的任何位置;

注意

在其他标记之外 

从第一个链接和

[定义:Markup采用开始标签,结束标签,...的形式

从第二个链接


4
2017-11-02 16:03



所以布局文件中的任何地方都不起作用,因为一切都在第一级内。这没多大用处! - steveh
@steveh不,他们 能够 如答案所示,在布局文件内。它们不能位于其他节点的开始/结束标记内。但他们 能够 在根元素的开始/结束标记内。 - codeMagic


您应该知道xml文件中的注释被认为是XmlComment类型的节点,因此如果您加载xml文件,那些节点将被加载,您可以在解析加载的内容时避免它们或过滤它们。

所以,这将是正确的格式,

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton" 
android:layout_alignParentTop="true"
android:src="@drawable/gfx_select_medium"
android:layout_marginRight="22dp"/>    
<!-- android:background="?android:attr/selectableItemBackground" -->

或者你可以看到这个 链接..


2
2017-11-02 16:08