只是在 androids 教程上完成 hello world 并尝试在 main.xml 文件上放置按钮后立即添加按钮保存文件我收到黄色警告
[I18N] Hardcoded string "Button", should use @string resource this come up after saving the file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
最佳答案
警告不会阻止您执行代码。它仍然告诉您在 Android 中不鼓励在布局中使用硬编码字符串。如果您想避免这种情况,您需要在 res\values
文件夹中创建文件 strings.xml
并在其中添加以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="button_string">Button</string>
</resources>
然后像这样更改布局文件:
...
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_string" />
惯例是导出您刚创建的文件中的所有文字字符串。这样国际化您的代码也会更容易。
顺便说一下,您已经有了在 TextView 中使用字符串的示例,因此只需在其中添加另一个常量即可。
关于android很快添加一个按钮得到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9549348/