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

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

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

In @+id/ the plus symbol

相关标签:
13条回答
  • 2020-11-22 02:09

    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.

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

    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"

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

    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

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

    Sometimes you see references in your layout files like:

    <listview id="@+id/android:list">

    and

    <listview id="@android:id/list">

    What's the difference?

    .. I'm glad you asked ☺

    @+id/foo means you are creating an id named foo in the namespace of your application. You can refer to it using @id/foo. @android:id/foo means you are referring to an id defined in the android namespace.

    The '+' means to create the symbol if it doesn't already exist. You don't need it (and shouldn't use it) when referencing android: symbols, because those are already defined for you by the platform and you can't make your own in that namespace anyway.

    This namespace is the namespace of the framework. for example, you need to use @android:id/list because this the id the framework expects to find.. (the framework knows only about the ids in the android namespace.)

    Completely copied from this source

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

    Its very simple:

    "@+..." - create new

    "@..." - link on existing

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

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

    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>
    
    0 讨论(0)
提交回复
热议问题