这documentation总是让我困惑:
For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist. For example:
<TextView android:id="@+id/nameTextbox"/>
我从事编程已有一段时间了。但是,我从未遇到过必须使用不带加号的 ID 声明的情况。这也是违反直觉的。 ID 应该是唯一的!
有什么好的用例吗?为什么要重复使用相同的资源 ID 名称?
最佳答案
@+id/name 当你创建一个new id
"@id/" 当您链接到现有 id
用例示例 1:
假设您在 XML 中创建了自己的资源:
<resources>
<item name="plusIcon" type="id"/>
</resources>
现在您可以在多个地方使用此资源,而无需使用 @+id 创建新资源。说 layout_one.xml
:
<TextView android:id="@id/plusIcon"/>
layout_two.xml
中的相同资源:
<TextView android:id="@id/plusIcon"/>
用例示例 2:
Android 框架还提供了许多其他 ID 资源。如果你想在这种情况下引用 Android 资源 ID,你可以使用 @android 你不需要创建你自己的新资源 ID
关于android - 没有加号的新资源 id 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50961540/