DevEco Studio中警告"Hardcoded String ‘中文’, should use ‘$string:中文’ resourse"的解决方式
例如在xml文件中
<Button
ohos:id="$+id:gomoku_button"
ohos:width="200fp"
ohos:height="70fp"
ohos:text_size="27fp"
ohos:text="中文"//此处有警告
ohos:text_color="white"
ohos:background_element="$graphic:genre_background_button"
ohos:left_margin="5vp"
ohos:bottom_margin="25vp"
ohos:right_padding="8vp"
ohos:left_padding="8vp"
/>
解决方式:

在此路径下的string.json的"string"中新增
{
"string": [
...
{
"name": "zhongwen",
"value": "中文"
},
...
]
}
修改之前的代码为:
<Button
ohos:id="$+id:gomoku_button"
ohos:width="200fp"
ohos:height="70fp"
ohos:text_size="27fp"
ohos:text="$string:zhongwen"//修改后
ohos:text_color="white"
ohos:background_element="$graphic:genre_background_button"
ohos:left_margin="5vp"
ohos:bottom_margin="25vp"
ohos:right_padding="8vp"
ohos:left_padding="8vp"
/>
本文介绍了如何处理DevEcoStudio中关于硬编码字符串的警告,提供了一种将中文文本转化为使用$string资源的解决方案。通过在string.json文件中新增对应字符串资源,并在xml代码中引用,可以消除警告并实现代码的资源本地化,提高应用的国际化兼容性。
988

被折叠的 条评论
为什么被折叠?



