ID settings for controls in an Android----XML file

Source: Internet
Author: User

Android open in order to get a reference to the control in the layout file, the control must set the ID property, two ways to set the ID: (1) @+id/xxxx; (2) @id/xxxx; The following is a brief introduction.

    1. @+id/xxx: If the ID is not in the R file, it is created;

Note: There cannot be two two controls in an XML file that set the same ID in that form (with the include tag exception);

Example 1 correct use:

<textview android:id= "@+id/mytv" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:text= "Hello World"/>

Example 2 error (two IDs are the same): The system alerts you to errors at this time

<textview android:id= "@+id/mytv" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:text= "Hello World"/><textview android:id= "@+id/mytv" android:layout_width= "Wrap_content" Android:lay out_height= "wrap_content" android:text= "Hello World"/>

Example 3 allows the following usages, but the ID points to an include tag, after which the operation of the LinearLayout setting ID is meaningless:

<include android:id= "@+id/include1" layout= "@layout/my" android:layout_width= "50DP" android:layout_height= " 50DP "/><linearlayout android:id=" @+id/include1 "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:orientation=" Horizontal "/>

If the include tag is swapped with the LinearLayout, an error will be placed.

Example 4 allows the following usages, but the ID points to TextView, after which the include tag and the LinearLayout setting ID are meaningless:

<textview android:id= "@+id/mytv" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a    ndroid:text= "Hello World"/><include android:id= "@id/mytv" layout= "@layout/my" android:layout_width= "50DP" android:layout_height= "50DP"/><linearlayout android:id= "@id/mytv" android:layout_width= "Wrap_content" and roid:layout_height= "wrap_content" android:orientation= "Horizontal"/>

If you move the TextView position down, the run error occurs. If the layout referenced in the include has the same ID setting as TextView, there is no error but no meaning.

[Email protected]/xxxx: reference the corresponding ID in Ids.xml, unlike @+id/xxx, once an ID is added to the Ids.xml file, a corresponding ID is generated in the R.java file, regardless of whether the control uses that ID.

Examples of Use:

(1) Create Ids.xml

<resources> <item name= "Hello" type= "id"/> <item name= "Hello2" type= "id"/> <item name= "hel Lo3 "type=" id "/> <item name=" Hello4 "type=" id "/> <item name=" Hello5 "type=" id "/> <item name= "Hello6" type= "id"/> <item name= "Hello7" type= "id"/> <item name= "Hello8" type= "id"/></resources >

(2) Use ID

<textview android:id= "@id/hello" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:text= "Hello 1"/><textview android:id= "@id/hello" android:layout_width= "Wrap_content" android:layout _height= "wrap_content" android:text= "Hello 2"/><textview android:id= "@id/hello" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:text=" Hello 3 "/>

Multiple controls can set the uniform ID in the same way, but the ID belongs to only the control that used the ID first.

This article is from the "Wauoen" blog, make sure to keep this source http://7183397.blog.51cto.com/7173397/1847317

ID settings for controls in an Android----XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.