“在XML文件中给代码加注释”请注意

来源:互联网 发布:淘宝推广工具有哪些 编辑:程序博客网 时间:2024/05/16 16:10

   今天,在写相对布局时,在控间里一添加注释,就直接报错,不管是用//还是<!-- -->都会报错。

现在终于弄明白了。

  1 android的XML文件(包括layout下的和values下的)注释一般采用 <!--注释内容 -->的方式进行,也就是说,采用//是行不通的,不信你可以试试看。


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

但是,在<RelativeLayout ></RelativeLayout>中,在注释的前面有一个“>”符号,我们能够在他中间进行注释,他的完整结构是

<? xml   version = "1.0"   encoding = "utf-8" ?>   
< RelativeLayout   android:id = "@+id/right"   
     xmlns:android = "http://schemas.android.com/apk/res/android"   
     android:layout_width = "fill_parent"   
     android:layout_height = "fill_parent" >   
  
     <!-- 在这里注释是没有问题的 -->   
     < TextView   android:id = "@+id/right_view1"   

         android:background = "@drawable/yellow"  

         android:layout_width = "fill_parent"   

         android:layout_height = "wrap_content"  

         android:text = "第二组第一项"   />   


     <!-- 在这里注释也是没有问题的 -->   


     < TextView   android:id = "@+id/right_view2"   
         android:background = "@drawable/blue"   
         android:layout_width = "fill_parent"   
         android:layout_height = "wrap_content"   

         android:layout_below = "@id/right_view1" 

           android:text = "第二组第二项"   />   

</ RelativeLayout >   
即只能在组件布局代码后,或者在组件的前面添加注释。
0 0
原创粉丝点击