When should you use `@ + id` instead of` @ id`?

I have a bunch of View in <merge> and I have included <merge> in RelativeLayout . I try to reference the identifiers of the included View to act as anchors for my other View s, but Eclipse complains that the identifiers are not resolved. I found a workaround using @+id , not @id , when I refer to them first, and not when I actually define the objects that they refer to. I already defined two ID in Style and in the included <merge> , where they are declared, so it feels a little inefficient if I repeat the definition of the identifier.

Is this the right way to do this? I assume this is bad because the β€œ+” is another initialization. My current hypothesis is that you should use @+id when you first use the identifier, and not when initializing the object that will represent the identifier, is a bit like C/C++ and how they require at least a function prototype in lines up to the actual code that uses this function.

One more question: when you use the Eclipse GUI builder, I noticed that they always use @+id , not @id . Is this acceptable because it seems to me ineffective; it’s as if the application would spend more time determining if the ID was declared in R.id

+4
source share
1 answer

Using the @+id format, it tells the Android compiler to assign an identifier to your element, but that is not the identifier itself. Therefore, if I use @+id/myNewId , the asset compiler will create a new identifier named myNewId and provide it with a number. A valid number can be obtained from your code as R.id.myNewId .

If you use @id , the compiler will look for R.id.id You can define your own identifier in XML files, as described here: http://developer.android.com/guide/topics/resources/more-resources.html#Id . You can create your own file in res/values/[your_filename].xml :

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

and then refer to @id_name for example

You can also use the identifier defined in the Android namespace: @android:id/empty

This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id

There are also some additional discussions here: android: id, which is a plus for

+3
source

Source: https://habr.com/ru/post/1390712/


All Articles