当前位置:编程学习 > Android >>

Hardcoded string “姓名”, should use @string resource警告

在布局文件中,文本的设置使用如下写法时会有警告:Hardcoded string “姓名”, should use @string resource

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名" />

虽然可以正常运行,但是这不是一个好习惯,
建议我们在xml文件当中用到的数字汉字等字符串,应该在res/values/strings.xml中设置:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="xingming">姓名</string>
</resources>

然后再回到原来的xml文件中,引用:

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/xingming"
        />

就行了。像之前的直接写中文也可以,只是不正规。

这样在支持多语言时也是很有用的。
另外,颜色的设置也最好在color.xm中类似设置
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,