What does “@+id” mean?

前端 未结 4 657
渐次进展
渐次进展 2021-02-01 23:02

I\'ve read through much of the Android documentation and I\'ve yet to find any statement that says what an id value prefix of \"@+id\" means. I know what \"@string\" and variat

相关标签:
4条回答
  • 2021-02-01 23:19

    The plus sign indicates that you are creating a new ID which does not exist. eg."@+id/xyz" . If you write "@id/xyz" , it indicates that you are referencing the View from another part of layout.

    0 讨论(0)
  • 2021-02-01 23:23

    That's the syntax for linking an Android XML layout element to your Java code. So if I want to display text in a TextView, I have to do this.

    Step one - define the layout

    <TextView
    android:id="@+id/SaveResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SaveResult"
    android:layout_x="16px"
    android:layout_y="190px"
    >
    </TextView>
    

    Then, in code, I use @+id to link the layout to the variable. Think of the @+id as a foreign key in a database.

    TextView lblSaveResult = (TextView)findViewById(R.id.SaveResult);
    

    Now, it's ready for use. When I assign text, it uses the @+id to see where to put it, and also the color, size, etc..

    lblSaveResult.setText("This text is now on the screen");
    

    Sorry, but I don't know where the documentation is for this...

    0 讨论(0)
  • 2021-02-01 23:31

    The plus sign simply indicates that the ID should be created if it doesn't exist.

    It's common practice to use @+id/foo when defining a new View in a layout, and then use @id/foo to reference the View from another part of the layout (say, in a RelativeLayout hierarchy) or R.id.foo to reference it from code.


    UPDATE: Docs are here: Declaring Layout - Attributes - ID

    0 讨论(0)
  • 2021-02-01 23:41

    The at sign (@) is required when you're referring to any resource object from XML. It is followed by the resource type (id in this case).

    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 gen/R.java file that refers to the UI 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.

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