我是靠谱客的博主 冷艳柚子,这篇文章主要介绍Hardcoded string “姓名”, should use @string resource警告,现在分享给大家,希望可以做个参考。

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

复制代码
1
2
3
4
5
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" />

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

复制代码
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="xingming">姓名</string> </resources>

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

复制代码
1
2
3
4
5
6
7
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/xingming" />

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

这样在支持多语言时也是很有用的。
另外,颜色的设置也最好在color.xm中类似设置。

最后

以上就是冷艳柚子最近收集整理的关于Hardcoded string “姓名”, should use @string resource警告的全部内容,更多相关Hardcoded内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部