【问题标题】:Why doesn't the "@string/" attribute not have a + sign?为什么“@string/”属性没有 + 号?
【发布时间】:2020-05-07 11:24:32
【问题描述】:

我知道视图的id 属性中的+ 符号表明它是一个新的用户定义的自定义值,必须添加到R.Java 文件中。但是,在下面的示例 XML 按钮视图属性中,为什么 @string 在单词 string 之前没有 + 符号,就像 @+id 属性一样?

<Button android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_button_text"/>

【问题讨论】:

  • 在这种情况下,您正在链接现有资源(字符串)。
  • @GabrieleMariotti 但这不是将添加到 R.Java 中的自定义字符串值吗?所以,这应该需要一个+ 符号,对吧?
  • @Swifty 没有。字符串已经存在于资源文件中(然后在 R.string.xxx 中)
  • @GabrieleMariotti 你的意思是这是因为字符串资源是一个预定义的静态值,而 R.Java 中的 id 值是可变的,每次在 Android 中构建和运行应用程序时都需要重新构建工作室?

标签: android android-xml-attribute


【解决方案1】:

加号 + 表示这是一个新的资源 ID,Android Asset Packaging Tool 将在 R.java 类中创建一个新的资源整数, 如果它尚不存在。

@string - 在资源文件中定义。 &lt;string&gt; 元素的名称用作资源 ID。

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多