简体   繁体   English

“@ + id”是什么意思?

[英]What does “@+id” mean?

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. 我已经阅读了很多Android文档,但我还没有找到任何声明,说明“@ + id”的id值前缀是什么意思。 I know what "@string" and variations of that mean, but not the variation with the "+". 我知道“@string”及其变体是什么意思,但不知道“+”的变化。 Besides giving me the answer, can you show me where in the Android docs this is documented? 除了给我答案之外,你能告诉我Android文档在哪里记录了吗?

The plus sign simply indicates that the ID should be created if it doesn't exist. 加号仅表示如果ID不存在,则应创建该ID。

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. 通常的做法是在布局中定义新的View时使用@+id/foo ,然后使用@id/foo从布局的另一部分(例如,在RelativeLayout层次结构中)或R.id.foo从代码中引用它。


UPDATE : Docs are here: Declaring Layout - Attributes - ID 更新 :文档在这里: 声明布局 - 属性 - ID

That's the syntax for linking an Android XML layout element to your Java code. 这是将Android XML布局元素链接到Java代码的语法。 So if I want to display text in a TextView, I have to do this. 因此,如果我想在TextView中显示文本,我必须这样做。

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. 然后,在代码中,我使用@ + id将布局链接到变量。 Think of the @+id as a foreign key in a database. 将@ + id视为数据库中的外键。

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.. 当我分配文本时,它使用@ + id来查看放置它的位置,以及颜色,大小等。

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

Sorry, but I don't know where the documentation is for this... 对不起,但我不知道文档在哪里...

The at sign (@) is required when you're referring to any resource object from XML. 当您从XML引用任何资源对象时,需要使用at符号(@)。 It is followed by the resource type (id in this case). 接下来是资源类型(在本例中为id)。

The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. 仅当您第一次定义资源ID时才需要资源类型之前的加号(+)。 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. 编译应用程序时,SDK工具使用ID名称在项目的gen / R.java文件中创建一个引用UI元素的新资源ID。 With the resource ID declared once this way, other references to the ID do not need the plus sign. 使用此方式声明资源ID后,对ID的其他引用不需要加号。 Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. 仅在指定新资源ID时才需要使用加号,而对于字符串或布局等具体资源则不需要。

The plus sign indicates that you are creating a new ID which does not exist. 加号表示您正在创建一个不存在的新ID。 eg."@+id/xyz" . 例如。“@ + id / xyz”。 If you write "@id/xyz" , it indicates that you are referencing the View from another part of layout. 如果您编写“@ id / xyz”,则表示您从布局的另一部分引用了View。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM