java - xml 中的字符串标记在 16 以上的 api 中产生了一些问题

标签 java android xml

我正在制作一个有英语和乌尔都语两种语言的应用程序。我有英语字符串文件,字符串名称为 When_undressing_ar

  <string name="When_undressing_ar">بِسْمِ اللهِ</string>

并且有单独的乌尔都语字符串xml用于乌尔都语语言,它的字符串标签为

<string name="لباس_اتارتے_وقت_کیا_پڑھیں_ar">بِسْمِ اللهِ</string>

并通过调用从字符串中获取这些值

private static String getDuwaElement(String nameame, Context context) {
int duwaId = context.getResources().getIdentifier(name, "string", context.getPackageName());

return context.getString(duwaId);
}

当语言设置为英语时,名称为“When_undressing_ar”。当语言设置为乌尔都语时,名称为“???_??????_ar”

一切正常,并为我提供了两种语言的所有资源,

但是,当我在 api 22 上尝试此应用程序时,它给出了未找到资源的异常。这在 api 16 之前都有效。但是在高于 16 的 api 上运行时会出现异常。请帮助解决导致此问题的原因,它会给出资源未找到的异常

最佳答案

完整的工作演示

主要 Activity

public class MainActivity extends AppCompatActivity {

    Context context;
    TextView textView;
    SharedPreferences preferences;
    String[] SuppertedLangCodes = {"en", "ur"};
    String key = "lang";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        preferences = getSharedPreferences("myprefs", MODE_PRIVATE);

        textView = (TextView) findViewById(R.id.foodName);
        textView.setText(getResources().getText(R.string.txt_name));
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (preferences.getString("lang", "").toString().equalsIgnoreCase("") || preferences.getString("lang", "").toString().equalsIgnoreCase(SuppertedLangCodes[0])) {

                    applyLanguage(context, SuppertedLangCodes[1]);
                    preferences.edit().putString(key, SuppertedLangCodes[1]).apply();
                } else {

                    applyLanguage(context, SuppertedLangCodes[0]);
                    preferences.edit().putString(key, SuppertedLangCodes[0]).apply();
                }
            }
        });
    }

    public void applyLanguage(Context context, String language) {
        android.content.res.Configuration config = new android.content.res.Configuration();
        // Since API level 17 or below cause issues with RTL, lets keep them LTR
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            config.locale = new Locale("en");
        } else {
            config.locale = new Locale(language);
        }
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        recreate();
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/white"
    android:orientation="horizontal"
    android:weightSum="10">


    <TextView
        android:id="@+id/foodName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="left"
        android:inputType="textCapWords"
        android:textColor="@color/colorPrimaryDark"
        android:textColorHint="@color/colorPrimaryDark"
        android:textSize="18sp"
        android:layout_marginLeft="20dp" />

</RelativeLayout>

Strings.xml正常

<resources>
    <string name="app_name">Gallery Test</string>
    <string name="txt_title_photo_gallery">Photo Gallery</string>
    <string name="txt_name">what to pray while undressing</string>

</resources>

您文件夹中的 Strings.xml

 <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

        <!--<string name="txt_name" translatable="false">pk.wiseapps.texter.sms.ps.GSM_SMS</string>-->
        <string name="txt_name">لباس_اتارتے_وقت_کیا_پڑھیں</string>

    </resources>

enter image description here

enter image description here

关于java - xml 中的字符串标记在 16 以上的 api 中产生了一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39290196/

相关文章:

java - xml 资源或 sqlite

java - 如何使用java验证XML?

java - 获取网页的压缩版本

Android Studio 有 2 个用于同一 Activity 的布局 Activity 文件。有必要吗?如何为所有较新的 SDK 版本保留 1 个文件?

android - 如何使用retrofit获取Json数据?

java - 单个 Activity 中的多个 Intent

SQL:如何获取 XML 数据类型中的属性值?

java - 如何在 Java Lettuce 中运行 Redis 原始命令?

java - 相对于 java.util.Scanner 中评估的正则表达式?

JavaFX StringConverter 不适用于组合框