Difference between “@id/” and “@+id/” in Android

前端 未结 13 1961
悲&欢浪女
悲&欢浪女 2020-11-22 01:20

What is the diffirence between the @id/ and @+id/?

In @+id/ the plus symbol

相关标签:
13条回答
  • 2020-11-22 01:57

    If the view item performs the same operation, you can use the @+id for each entry in any layout because during the compilation of multiple @+id/foo the R.java file only creates one enumeration. So for example, if I have a save button on each page that performs the same operation, I use android:id="@+id/button_save" in each layout. The R.java file only has one entry for the button_save.

    0 讨论(0)
  • 2020-11-22 01:58

    you refer to Android resources , which are already defined in Android system, with @android:id/.. while to access resources that you have defined/created in your project, you use @id/..

    More Info

    As per your clarifications in the chat, you said you have a problem like this :

    If we use android:id="@id/layout_item_id" it doesn't work. Instead @+id/ works so what's the difference here? And that was my original question.

    Well, it depends on the context, when you're using the XML attribute of android:id, then you're specifying a new id, and are instructing the parser (or call it the builder) to create a new entry in R.java, thus you have to include a + sign.

    While in the other case, like android:layout_below="@id/myTextView" , you're referring to an id that has already been created, so parser links this to the already created id in R.java.

    More Info Again

    As you said in your chat, note that android:layout_below="@id/myTextView" won't recognize an element with id myTextViewif it is written after the element you're using it in.

    0 讨论(0)
  • 2020-11-22 01:58

    In Short

    android:id="@+id/my_button"
    

    +id Plus sign tells android to add or create a new id in Resources.

    while

    android:layout_below="@id/my_button"
    

    it just help to refer the already generated id..

    0 讨论(0)
  • 2020-11-22 01:58

    Android uses some files called resources where values are stored for the XML files.

    Now when you use @id/ for an XML object, It is trying to refer to an id which is already registered in the values files. On the other hand, when you use @+id/ it registers a new id in the values files as implied by the '+' symbol.

    Hope this helps :).

    0 讨论(0)
  • 2020-11-22 02:04

    There's a bug with Eclipse where sometimes if you just created a new @+id/.., it won't be added immediately to the R.java file, even after clean-building the project. The solution is to restart Eclipse.

    This I think should be solved as soon as possible, because it may (and from experience, will) confuse some developers into thinking that there's something wrong with their syntax, and try to debug it even if there's really nothing to debug.

    0 讨论(0)
  • 2020-11-22 02:09

    @id/ and @android:id/ is not the same.

    @id/ referencing ID in your application, @android:id/ referencing an item in Android platform.

    Eclipse is wrong.

    0 讨论(0)
提交回复
热议问题