2011-10-17 139 views
24

如何评论android布局的xml文件。我看过Comments in Android Layout xml - 但我想在一位家长的评论。在android布局文件中评论

当我尝试这样做,我在日食得到一个错误:

<Button android:text="Button" 
<!-- this is a comment --> 
android:id="@+id/Discon" > 
</Button> 

从XML的背景知识,我才知道,我们不能在属性中添加注释。 有没有什么办法去除id作为属性并将其指定为元素?
或者有什么方法可以在元素属性中添加注释吗?

+0

指定为元素的手段? –

+0

例如android:id是Button元素(xml样式)的属性..我可以指定此属性作为button元素内的子元素吗? – ankith13

+0

只需检查下面的链接。它描述我们可以评论的方式 http://stackoverflow.com/a/3484726/5858287 – rakeshdev

回答

27

您不能在标签内嵌入注释。标签之间 如果你想临时注释掉一个属性(如果这就是你想知道),你必须把它移到ouside的标签,有评论它,它不是一个问题

<!-- 
    this is a comment 
--> 
<Button android:text="Button" 
    android:id="@+id/Discon" > 
</Button> 

<Button android:text="Button" 
    android:id="@+id/Discon" 
    > 
</Button> 


==> 

<!-- android:id="@+id/Discon" --> 

<Button android:text="Button" 
    > 
</Button> 
+0

谢谢你的答案,但它已经在我已经分享的文章中指定。但我只想知道是否有替代方案.. – ankith13

+0

这是在xml文件中添加注释的唯一方法 – Blackbelt

8

那么技术上没有评论,你买可以做

<Button comment="write whatever you want here" android:text="Button" ... 

或者,如果你想暂时删除属性,从名称中删除android:

基本上,Android会忽略没有android:名称空间的所有内容。

+0

感谢它的工作原理...我不知道这一点。 – ankith13

1

我使用Android Studio,所以我可以告诉你只有A.S的XML注释。

只需选择你想对此发表评论代码的一部分,

按(Ctrl + Shift +?)这将增加< 1--你的代码 - >,这里1(按住Shift + 1)(感叹号)

记住一两件事,这里有一个例外:例

<TextView 
      android:id="@+id/T_HomeworkMax1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FF1E0101" 
      android:textAppearance="?android:textAppearanceSmall" 
      android:layout_marginStart="95dp" 
      android:text="Max" /> 

    <TextView 
      android:id="@+id/T_HomeworkMin1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FF1E0101" 
      android:layout_marginStart="140dp" 
      android:textAppearance="?android:textAppearanceSmall" 

      android:text="Min" /> 

在这里,你不能选择的代码只有一两行,你必须选择完全节点(TextView的)。

<!-- <TextView 
      android:id="@+id/T_HomeworkMax1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FF1E0101" 
      android:textAppearance="?android:textAppearanceSmall" 
      android:layout_marginStart="95dp" 
      android:text="Max" />-->  

     <TextView 
      android:id="@+id/T_HomeworkMin1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FF1E0101" 
      android:layout_marginStart="140dp" 
      android:textAppearance="?android:textAppearanceSmall" 

      android:text="Min" /> 

现在,第一个TextView是注释。

0

XML不允许在标签()之间进行注释,这意味着您不能将注释放置到属性中,因为它们在标签内。 您无法删除某个属性并将其放置在其标签之外,因为它不再是属性。

总之,你试图做的事情,你试图做到这一点,它根本无法做到。