Introduction to the difference between @ id and @ + id and @ android: id in Android
- 2021-12-09 09:51:33
- OfStack
Preface
Yesterday, a new colleague suddenly asked me the difference between @ id and @ + id, why our projects are @ id's own new ui @ + id
Here I said that my simple reply project is before maintenance is for unified 1 management using @ id easy to modify, because in ids. xml there is a reference, @ + id is new, do not write like that, feel a little troublesome, but in order to unified 1 finally modified to @ id, may be some of these said vague, the following is my finishing 1
Knowledge, easy to find
First of all, we need to know that the id we usually use is of int type
1 @ + id:
What we often use is that when a layout file is modified and saved, the system will automatically generate the corresponding int type variable in R. java file. The variable name is the value after "/"
For example
android:id="@+id/tv"
The variable is named tv
2 @ id means that an existing ID is referenced, and an ids. xml is created in R. java, such as value in res
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="text_id_text" type="id" />
</resources>
Our id reference
<TextView
android:id="@id/text_id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Test 1 Sub-text " />
For the convenience of modifying id, if many interfaces use the same id, we can write this (about 30 interfaces in the maintenance project use the same id)
3 @ android: id This is a reference system id itself has not been used basically, simple understanding
android: id= "@ + id/btn", which means adding a control index with id as btn in R. java file, and the most commonly used way to declare id. android: id= "@ android: id/tabhost", which means that the existing ID of the system is referenced in ids. xml under the corresponding sdk directory. 1 is not called externally, but is used when calling internally. . android: id= "@ id/btn", which means to refer to an existing ID in R. java. For example, we built an ids. xml ourselves, which declares a group of id, one of which is btn, so you can refer to it like this.
Summarize