android很快添加一个按钮得到错误

标签 android

只是在 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/

相关文章:

android - 从 Android 中的 onActivityResult 中提示语音识别

java - 如何计算是/否回答的百分比?

android - 如何通过微调器选择使编辑文本可编辑和不可编辑

android camera2 api - 如何将公司 Logo 添加到录制的视频文件中

android - Firebase Android ServerValue.Timestamp 映射到 Long

android - 在 Android 中的 playTogether AnimatorSet 中对连续 AnimatorSet 进行动画处理

android - 实现 Google Cloud Messaging 时 Web 服务器出现异常

java - 重启 Android 后自定义小部件中的 Android 持久可检查菜单

android - 如何获取附近所有可发现的蓝牙设备的列表?

android - 在 Android Studio 中单独的 Mipmap 文件夹