android - 无法在 strings.xml 文件中放置格式说明符

标签 android

<分区>

我的应用支持英语和阿拉伯语。因此,我将两种语言的字符串分别放在 strings.xml 文件中。我需要一个格式化的字符串,为此我在 strings.xml 文件中使用了格式说明符(见下面的字符串)。我可以在英语文件中正确放置格式说明符,但无法在阿拉伯语文件中正确放置格式说明符。

<string name="device_offline_message" formatted="false">%s is offline. Please make sure it is online to execute the action.</string>

在阿拉伯语中,我无法将字符串放在字符串的末尾。即使我在文本的开头指定了格式化程序,lint 检查也会在 java 代码中显示错误,如

Format string 'device_offline_message' is not a valid format string so it should not be passed to String.format

在下面我使用字符串的代码行中

showToastAlertDialog(String.format(getString(R.string.device_offline_message), device.getShortName()));

最佳答案

使用

<string name="device_offline_message" formatted="false"> %1$s is offline.  Please make sure it is online to execute the action.</string>

代替

<string name="device_offline_message" formatted="false">%s is offline.  Please make sure it is online to execute the action.</string>

就像@Raghavendra 在评论中所说的那样。

并确保您已正确设置 Android Studio。

在android Studio 2中,默认不开启RTL支持,手动配置:

  1. 在您的计算机中,转到 [android-studio2.0]/bins/idea.properties

  2. 将 editor.new.rendering=true 添加到 idea.properties 的末尾

  3. 重启你的 android Studio

此处提供更多信息:How to edit arabic string properly?

关于android - 无法在 strings.xml 文件中放置格式说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40283325/

相关文章:

android - 在模拟器中运行的 React Native 导致 bundle 失败 : permission denied error

java - 在运行时将数据分配给四维数组

java - 如何在Android项目中命名getter/setter

android - 如何清除 Eclipse Android 模拟器上的缓存

java - 在Java Android的一个类中使用同名方法

安卓 : Showing keyboard hides listview contents

java - Android,即使每个位置的精度检查为 20,位置也会不断变化

android - 为什么仍然无法显示重置图像

android - 使用 Gradle 的 Travis-CI Android 测试不断超时

android - 为什么我的 Android SQLite 数据库突然损坏了?