【问题标题】:hardcoded sting "x", should use @string resource硬编码的 sting "x",应该使用 @string 资源
【发布时间】:2014-11-15 19:39:55
【问题描述】:

我在布局文件夹中的 xml 文件中收到此错误。我将其更改为 android:text="@string/x" 但随后它在按钮上显示:

这是我的 main_menu.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dp"
    android:layout_height="match_parent"
    android:layout_marginLeft="100dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Menu" //warning here
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/one_player"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="1player" />//warning here

    <Button
        android:id="@+id/two_player"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="2players" />//warning here

    <Button
        android:id="@+id/exit_game"
        android:layout_width="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_height="115dp"
        android:text="exitgame" />//warning here

</LinearLayout>

【问题讨论】:

    标签: java android xml eclipse


    【解决方案1】:

    需要在res->values->string.xml下定义字符串

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string
            name="player_1"
            1player</string>
    </resources>
    

    通过这种方式,您可以通过以下方式在任何视图下引用您的xml布局文件中的字符串值:-

    <Button
        android:id="@+id/one_player"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/player_1" />
    

    您可以参考link 了解有关如何在资源中声明字符串的更多详细信息。

    【讨论】:

      【解决方案2】:

      您似乎忘记在strings.xml 中实际声明变量。

      【讨论】:

      • 我确实忘记在strings.xml中声明它们了,我太笨了
      • 这篇文章应该是评论。没有答案。
      猜你喜欢
      • 2012-02-03
      • 2020-08-16
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2012-05-28
      相关资源
      最近更新 更多