我在布局文件夹中的 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>
最佳答案
需要在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/