android的XML文件注释一般采用 <!--注释内容 -->的方式进行,但是有时候却不能进行注释,如
- <Button
- android:id="@+id/button2"
- android:layout_width="fill_parent"
- <!-- 控件高度 -->
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="LinearLayout和RelativeLayout互助使用" />
- 释会使得注释后面的代码不能运行,这是为什么呢?在网上没有找到说明,通过翻阅书籍,终于知道原因了
在XML中,形如 <Button /> 的表示方式,其中“/>”的含义表示这个XML中没有内文,他是一个最小组成单元,也就是说他的中间不能包含其他任何< >的代码,所以在<Button />中间注释会出现错误
对比整个XML文件,我们为什么可以在其中间注释呢,看代码
- <?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>
注意看到,在注释的前面有一个“>”符号,这就是我们能够在他中间进行注释的原因,他的完整结构是
<RelativeLayout ></RelativeLayout>
这就不是最小组成单元的表示方式了
摘自:http://blog.youkuaiyun.com/kkkding/article/details/8616511