android - 没有加号的新资源 id 声明

标签 android android-resources

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/

相关文章:

android - 使用 colorAccent 属性的资源 NotFoundException getColor()

android - 后退按钮在父级上调用 onDestroy()

java - Android 动画 Alpha

android - 从 firebase 检索特定数据

在纵向和横向之间切换时,Android整数xml资源值不会改变

android - 根据android中的性别从string.xml资源文件加载

android - 检索字符串和字符串数组资源的区别?

android - 通过 .g3db 文件处理 Bullet 中的碰撞

android - 从 android 项目中删除所有未使用的资源

java - 从 Android 原始资源文件中扫描句子