java - 硬编码的刺 "x",应该使用 @string 资源

标签 java android xml eclipse

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

这是我的 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>

最佳答案

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

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

通过这种方式,您可以通过以下方式在任何 View 下引用 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有关如何声明资源中的字符串的更多详细信息。

关于java - 硬编码的刺 "x",应该使用 @string 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26949896/

相关文章:

java - 拦截方法调用

java - 覆盖 Java 中的 protected 方法

java - 将指向外部互联网站点的超文本链接放入 javadoc

android - runnable 中的 handler.postDelayed 在 Kotlin 中显示语法错误 - Android

java - 屏幕锁定时我无法接收通知

java - 在 Java 中以编程方式创建 W3C DOM 的有效方法是什么?

c# - XDocument 保存删除节点前缀

java - 如何计算多个字符串中某个字符出现的次数?

android - YouTube iframe 嵌入无法在 Android 上自动播放

xml - Apache Ant 不会做数学运算