跳转至

Android 多语言自动适配

更新日期: 2026-01-15
  • 2026-01-15 更新例子
  • 2022-01-16 创建文档

Android为多语言适配提供了很大的方便。开发者不需要在代码中进行修改。只需要配置xml文件。

res --> values 其中存放有xml文件。一般这些都是英文的字符串。我们可以存放其他语言的字符串。

另一语种的字符串文件放在另外的文件夹下。文件夹命名规则为: values-##-r**

例如: values-zh-rCN

其中##表示语言代号(language codes),**表示国家代号(country codes),也可以只有语言代号。

步骤如下:

  • 1.在res文件夹下新建一个文件夹,命名为values-zh-rCN
  • 2.在values-zh-rCN文件夹中新建一个string.xml文件,里面存放的是程序中用到的字符串。

注意转义问题

比如要显示英文: rustfisher's website

在xml中应该加上斜杠转义符 \

<string name="settings">rustfisher\'s website</string>

如果不加会编译报错 app:mergeDebugResources

does not contain a valid string resource.
Failed to flatten XML for resource 'settings' with error: Invalid unicode escape sequence in string
"{str}"

.....

Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.ResourceCompilerRunnable
   > Resource compilation failed (Failed to compile values resource file /Users/rustfisher/Desktop/ws/new-android/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values-en/values-en.xml. 
   Cause: java.lang.IllegalStateException: Can not extract resource from com.android.aaptcompiler.ParsedResource@14c0ae15.,Can not extract resource from com.android.aaptcompiler.
   ParsedResource@2ef165e4.,Can not extract resource from com.android.aaptcompiler.ParsedResource@36387e8f.,Can not extract resource from com.android.aaptcompiler.ParsedResource@1e65102a.,Can not extract resource from com.android.aaptcompiler.ParsedResource@fc4a5f5.,Can not extract resource from com.android.aaptcompiler.ParsedResource@2abf3fac.,Can not extract resource from com.android.aaptcompiler.ParsedResource@5f06e072.,Can not extract resource from com.android.aaptcompiler.ParsedResource@69d45ff8.). Check logs for more details.

本站说明

一起在知识的海洋里呛水吧。广告内容与本站无关。如果喜欢本站内容,欢迎投喂作者,谢谢支持服务器。如有疑问和建议,欢迎在下方评论~

🖥️云雨服务器 🙋反馈问题 🔥最近更新 🍪投喂作者