gpt4 book ai didi

android - 资源和布局方向仅在 Android 8.0 及更高版本上呈现不正确

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:19 46 4
gpt4 key购买 nike

我有一个多语言应用,主要语言是英语,第二语言是阿拉伯语。

我在我的应用程序中的每个 ActivityonCreate() 中调用 setLocale():

public static void setLocale(Locale locale){
Locale.setDefault(locale);
Context context = MyApplication.getInstance();
final Resources resources = context.getResources();
final Configuration config = resources.getConfiguration();
config.setLocale(locale);
context.getResources().updateConfiguration(config,
resources.getDisplayMetrics());
}

其中 locale 是以下之一:

![enter image description here

上述方法 super.onCreate(savedInstanceState) 被调用之前被调用。

documentation 中所述,

  • 我在 list 中添加了 android:supportsRtl="true"
  • 我已将所有具有 leftright 属性的 xml 属性分别更改为 startend。<
  • 我已将阿拉伯语字符串放入 res\values-ar\strings 文件夹,并将可绘制资源放入 res\drawable-ar 文件夹(其他资源也类似) .

以上设置工作正常。将 Locale 更改为 ar-AE 后,阿拉伯语文本和资源在我的 Activity 中正确显示。

但是,对于 8.0 及以上版本的所有 Android 设备,资源和布局方向都存在问题。

在版本低于 8.0 的设备上,RTL 屏幕正确地如下所示:

enter image description here

在所有 8.0+ 的设备上,相同的屏幕显示如下:

enter image description here

这是错误的

It turns out that both the direction and the resources are getting displayed incorrectly.

这里有两个问题:

  • 正确的 Locale 似乎没有在整个应用程序配置中更新。
  • 文本和可绘制对象的方向与应有的方向相反。

关于方向,一个奇怪的方法叫做 setLayoutDirection()存在我以前没有注意到的。

我想知道这个问题是什么,为什么会出现在 Oreo 中,解决方案是什么。请对此提供帮助/评论。

编辑:

According to the API Differences report, the updateConfiguration() method was indeed deprecated in Android 7.1 (API level 25).

此外,找到了所有相关的帖子。按重要性排序:

1. Android N change language programmatically .

2. Android context.getResources.updateConfiguration() deprecated .

3. How to change Android O / Oreo / api 26 app language .

4. Android RTL issue in API 24 and higher on locale change

5. Change language programmatically (Android N 7.0 - API 24) .

6. Android N - Change Locale in runtime .

7. RTL layout bug in android Oreo .

最佳答案

updateConfiguration()方法被弃用

现在我们需要使用createConfigurationContext()

我是这样处理的

create a new class ContextWrapper

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;

import java.util.Locale;

public class ContextWrapper extends android.content.ContextWrapper {

public ContextWrapper(Context base) {
super(base);
}

public static ContextWrapper wrap(Context context, Locale newLocale) {

Resources res = context.getResources();
Configuration configuration = res.getConfiguration();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);

LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);

context = context.createConfigurationContext(configuration);

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);

} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}

return new ContextWrapper(context);
}}

create a new class of BaseActivity

import android.content.Context;

import android.support.v7.app.AppCompatActivity;

import java.util.Locale;

/**
* Created by nilesh on 20/3/18.
*/

public class BaseActivity extends AppCompatActivity {

@Override
protected void attachBaseContext(Context newBase) {

Locale newLocale;

String lang = new PrefManager(newBase).getLanguage();

if (lang.equals("zh_CN")) {
newLocale = new Locale("zh");
} else {
newLocale = new Locale(lang);
}


Context context = ContextWrapper.wrap(newBase, newLocale);
super.attachBaseContext(context);
}
}

Create a PrefManager class to store locale

import android.content.Context;
import android.content.SharedPreferences;

public class PrefManager {

private SharedPreferences.Editor editor;
private Context mContext;
private SharedPreferences prefs;
private final String LANGUAGE = "language";
private final String PREF = "user_data";

public PrefManager(Context mContext) {
this.mContext = mContext;
}

public String getLanguage() {
this.prefs = this.mContext.getSharedPreferences(PREF, 0);
return this.prefs.getString(LANGUAGE, "en_US");
}

public void setLanguage(String language) {
this.editor = this.mContext.getSharedPreferences(PREF, 0).edit();
this.editor.putString(LANGUAGE, language);
this.editor.apply();
}

}

Now you need to extends your BaseActivity in your all activity like

public class OrdersActivity extends BaseActivity

Now when your need to change Locale just update the value in PrefManager and restart your activity

    PrefManager prefManager= new PrefManager(this);
prefManager.setLanguage("zh_CN");
// restart your activity

注意

You can download source code from github repo

关于android - 资源和布局方向仅在 Android 8.0 及更高版本上呈现不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52187959/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com