如何在Android的XML文件中添加注释 本文转载自http://blog.csdn.net/pipisorry/article/details/24833325,感谢原创大神!

来源:互联网 发布:win7与linux双系统 编辑:程序博客网 时间:2024/05/16 11:37
http://blog.csdn.net/

怎么在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>

相关错误解决

Hardcoded string "???", should use @string resource

在布局文件中,文本的设置使用如下写法时会有警告:Hardcoded string "下一步", should use @string resourcecopy

  1. <Button  
  2.         android:id="@+id/button1"  
  3.         android:layout_width="118dp"   
  4.         android:layout_height="wrap_content"  
  5.         android:text="下一步" />"  
虽然可以正常运行,但是这不是一个好习惯(如果你有个string="确定"在多个地方都用到,但是后来你想把它改为“确认”,你觉得是找到引用它的地方一个个改方便呢,还是直接在strings.xml里面改一处方便?你只看到了strings中增加了一条记录),应该在res/values/strings.xml中设置:copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="message">下一步</string>  
  4. </resources>  
引用的时候使用
<span class="atn">android:text</span><span class="pun">=</span><span class="atv">"@string/message"</span>
就行了。这样做可以做到一改全改,在支持多语言时也是很有用的。另外,颜色的设置也最好在color.xm中类似设置。/article/details/24833325
阅读全文
0 0
原创粉丝点击