当设备语言更改时,TextView 显示 strings.xml 中不存在的字符串

2024-01-06

当我更改设备语言时,strings.xml 中没有的字符串将显示在我的文本视图中。

下面是我的文本视图代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="@dimen/padding_all_activity_16"
    android:text="@string/check_document_image_readable"
    android:textSize="18sp"
    android:textStyle="bold" />
......

在values/string.xml 文件中:

    <string name="check_document_image_readable">A foto ficou legível?</string>

我的应用程序不支持其他语言(它是用葡萄牙语编写的)。

When I run the app withe the device set to English: original

But if I change to Spanish (even if I kill the App): enter image description here

我正在使用 Android Studio 3.0.1 进行开发。这种情况发生在 API 24 和 25 中。

如何对任何设备语言仅使用values/strings.xml?


Android 支持不同的语言和文化 https://developer.android.com/training/basics/supporting-devices/languages.html根据设备的语言设置。您可以使用您希望支持的语言的不同字符串资源来实现这一点。

在您的项目中,右键单击 res/values/ 并选择新建 -> 值资源文件。输入“strings”作为文件名,并将源选项设置为 main。在可用限定符中,单击“>>”按钮将“区域设置”移至“选择的限定符”部分。选择您希望支持的语言并选择区域(默认为:任何区域),然后左键单击“确定”。 Android studio 会自动生成一个新的资源文件,也称为“strings.xml”。

我为什么提到这一点? Android 在运行时查找默认字符串资源。可能发生的情况是您没有使用默认字符串资源,而是使用“string-pt”资源,如果您没有西班牙语或其他语言的资源字符串文件,这将是一个问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当设备语言更改时,TextView 显示 strings.xml 中不存在的字符串 的相关文章

随机推荐