开发者

Difference between "@id/" and "@+id/" in Android

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

In @+id/ the plus symbol + instructs to create a new resource name and add in to the R.java file but what about @id/? From the documentation of ID: when referencing an Android resource ID, you do not need the plus symbol, but must add the android package namespace, like so:

android:id="@android:id/list"

But in the image below Eclipse doe开发者_运维知识库sn't suggest any kind of @android:id/.

Difference between "@id/" and "@+id/" in Android

Are @id/ and @android:id/ the same?


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.


the + sign is a short cut to add the id to your list of resource ids. Otherwise you need to have them in a xml file like this

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


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..


Its very simple:

"@+..." - create new

"@..." - link on existing

Source: https://developer.android.com/guide/topics/resources/layout-resource.html#idvalue


The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.

From: https://developer.android.com/training/basics/firstapp/building-ui.html


From the Developer Guide:

android:id="@+id/my_button"

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:

android:id="@android:id/empty"


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.


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 :).


@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.


Difference between @+id and @id is:

  • @+id is used to create an id for a view in R.java file.
  • @id is used to refer the id created for the view in R.java file.

We use @+id with android:id="", but what if the id is not created and we are referring it before getting created(Forward Referencing).

In that case, we have use @+id to create id and while defining the view we have to refer it.

Please refer the below code:

<RelativeLayout>

     <TextView
        android:id="@+id/dates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/spinner" />

   <Spinner
     android:id="@id/spinner"
     android:layout_width="96dp"
     android:layout_height="wrap_content"
     android:layout_below="@id/dates"
     android:layout_alignParentRight="true" />

</RelativeLayout>

In the above code,id for Spinner @+id/spinner is created in other view and while defining the spinner we are referring the id created above.

So, we have to create the id if we are using the view before the view has been created.


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.


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

The first one is used for to create the ID of the particular ui component and the another one is used for to refer the particular component

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜