如何在android的XML文件中添加注释

怎么在android的XML文件中添加注释

android的XML文件注释一般采用 <!--注释内容 -->的方式进行

在XML中,形如    <Button           />      的表示方式,其中“/>”的含义表示这个XML中没有内文,他是一个最小组成单元,也就是说他的中间不能包含其他任何< >的代码,所以在<Button />中间注释会出现错误

注意看到,在注释的前面有一个“>”符号,这就是我们能够在他中间进行注释的原因,他的完整结构是

<RelativeLayout ></RelativeLayout>

这就不是最小组成单元的表示方式了

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout android:id="@+id/right"  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.   
  7.     <!-- 在这里注释是没有问题的 -->  
  8.     <TextView android:id="@+id/right_view1"  
  9.         android:background="@drawable/yellow" android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" android:text="第二组第一项" />  
  11.     <!-- 在这里注释也是没有问题的 -->  
  12.     <TextView android:id="@+id/right_view2"  
  13.         android:background="@drawable/blue"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_below="@id/right_view1" android:text="第二组第二项" />  
  17. </RelativeLayout>  

即只能在组件布局代码后,或者在组件的前面添加注释。如下所示:

<RelativeLayout
        android:id="@+id/item_layout"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <!--  -->
        <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
                  <!--  -->
        </LinearLayout>
</RelativeLayout>