Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

syntax - When are closing tags required with Android view markup?

For some Android XML attributes, you don't conclude the opening tag with a '>' until after you've inserted your formatting components. For example:

<EditText 
  android:id="@+id/etEmails"> 
</EditText>

Why is there no definition for the EditText component within the opening and closing tags? Also, I noticed that some don't even require closing tags and are just in themselves XML statements. For example:

<Button
  android:text="Subtract 1"
  android:id="@+id/buttSub"
  />

Why does this XML statement not require a closing statement when it practically provides the same components as the EditText field?

  • Is there a failsafe way of knowing which ones require opening and closing statements for proper syntax?

  • Is there a list/reference for which ones do and don't?

  • What's the difference between these different components?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The <Button /> is a so called short tag. This is safe.

If a tag has no body you can obmit the closing tag and add a slash at the end of the tag which means here this tag has no children.

This notation is very common in xhtml for <br /> and <img src="" alt="" /> tags.

The benifit is that you don't need to write the closing tag which makes it simpler to read and if you have a huge xml file there are less data to transport. (This does not count for Android is this case, because the android SDK procudes internally a binary file.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...