Commenting in activity_main.xml

2019-08-03 02:17发布

问题:

I am new to Android. I understand that commenting in XML works the same as it does in HTML, using the

<!-- comment here -->

form. I would like to write some comments in my activity_main.xml configuration file for an Android project, but it's giving me errors. It's worth noting that I'm using Eclipse, but for the moment, I'm editing the XML file directly as opposed to graphically because I'd rather force myself to understand the attributes. I am trying to comment in and out conflicting attributes but it's giving me red.

Is there a way for Eclipse to allow me to comment that particular XML file?

For those that asked, here's an example:

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" 
    android:layout_weight="1" />

Say I'd like to simply comment out that second line. I want to comment out the layout_width tag because I'm later using layout_weight. When I try to comment it out, I get:

Element type "EditText" must be followed by either attribute specifications, ">" or "/>".

One person responded that a comment can't break up a tag, which is what I intended to do. I thought I had done that before in HTML, so I assumed that XML abided by the same rules. I guess not, or maybe I just need to brush up on my XML.

回答1:

The problem with commenting attributes on xml is that you can't have a comment break the xml tag. So if you have:

<View 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
/>

You can't comment layout_width, your comment must be outside of the view tag.



回答2:

There are two things forbidden in terms of commenting in XML:

  • double -- inside comments
  • comments inside tags

So this code would generate an error.

<EditText android:id="@+id/edit_message"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:hint="@string/edit_message" 
  <!-- That would use all empty space -->
  android:layout_weight="1" />

And this too:

<!-- This is for -- your name -->
<EditText android:id="@+id/edit_message"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:hint="@string/edit_message"       
  android:layout_weight="1" />

You could read more about comments in Android: http://android4beginners.com/2013/07/appendix-d-comments-in-xml-and-java-its-crucial-to-create-a-documentation-of-android-app/



回答3:

It is giving you the error because you are closing your EditText tag in the second line itself.

That's because for commenting out some code, you'd use <!-- -->, so ">" has been taken. and from next line onward, it's not having the starting "<".

For example,

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

If you comment out the second line, i.e. layout_width, it will be considered as though you were closing the TextView tag, and next line onward it would not have any starting "<".