On the difference between android @ id and @ + id

  • 2021-12-09 09:51:40
  • OfStack

Today, let's talk briefly about the difference between @ id and @ + id in android.

Before, I used @ + id in any situation in my layout, but later I found that some codes used @ id, and I didn't know the difference between the two. So I searched for information on the Internet and finally solved the problem. Record 1 here.

Components in Android need to be represented by a value of type int, which is the value of the id attribute in the component tag. The id property can only accept values of resource type, that is, values that must begin with @, for example, @ id/abc, @ + id/xyz, and so on.

If you use "+" after @, it means 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, @ + id/xyz generates int xyz = value in the R. java file, where value is a 106-ary number. If xyz already has a variable with the same name in R. java, no new variable is generated and the component uses the value of the existing variable.

That is, if you use the @ + id/name form, when a variable named name exists in R. java, the component uses the value of that variable as an identity. If the variable does not exist, add a new variable and assign the corresponding value to it (it will not be repeated).

Since the component's id attribute is one resource id, it is natural to set any existing resource id value, for example, @ drawable/icon, @ string/ok, @ + string/you, and so on. Of course, you can also set the existing resource id in android system, for example, @ id/android: list proposed by the landlord, so what does this android mean? In fact, this android is the package where the R class of the system (in R. java file) is located. We can enter android. R. id in the Java code editing area, and the corresponding resource id will be listed. For example, we can also set the id property value to @ id/android: message.


<ListView android:id="@+id/android:message"
  android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

There is another way to view the id defined in the system and enter < android sdk Installation Directory > Directory\ platforms\ android-1. 5\ data\ res\ values, find the file ids. xml and open it as follows:


<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="price_edit">false</item>
<item type="id" name="amount_edit">false</item>
</resources>

If ID is defined in ids. xml, @ id/price_edit can be defined in layout as follows, otherwise @ +id/price_edit

Let's talk briefly here. In fact, @ + id is to add an id name to the R. java file. If the same id name already exists before, the previous name will be overwritten. While @ id is an id resource that directly references the existence of R. java file. If it does not exist, it will compile and report an error.

Take an example:


<View
  android:layout_below="@+id/view1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

<View
  android:id="@id/view1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

The above code is correct, that is, the following order is allowed. But remove the + sign on line 1, or change the position between line 2 and line 1 by one, and it won't work! ! !


android:layout_below="@+id/view1"
android:id="@id/view1"

The principle is:

@ + id adds a new id to R. java file, which is why the control can be found with findViewById (R. id. xxx), and @ id looks directly in this file.

And why does an id already exist, and it can still @ + id in layout_below?
Because android can tolerate the existence of duplicate id, it doesn't mean that there will be two identical id, but the new id overrides the original id, and here @ + id just creates id once again.

It is not recommended to use @ + id in the layout when an id already exists. Although this is allowed for the time being, this writing is not standardized and may be prohibited in subsequent android versions.

To put it simply, in fact, in the relative layout, except for declaring that the id of the control uses @ + id/, it is best to use @ id for other reference controls, which conforms to the code specification, saves compilation time and avoids useless writing of R. java files.


Related articles: