Android 将应用程序中的区域设置自动更改为英语

Android 将应用程序中的区域设置自动更改为英语,android,localization,arabic,Android,Localization,Arabic,我在应用程序中集成了Locale(en和ar)。一旦我将应用程序更改为阿拉伯语(ar),然后关闭并打开我的应用程序,显示布局方向显示为阿拉伯语,但字符串加载为英语 这是我在点击按钮时更改语言的代码 public static void setLocale(final Context ctx, final String lang) { Log.d(TAG, "##Changing Language to: " + lang); AppSettings.getIn

我在应用程序中集成了Locale(
en
ar
)。一旦我将应用程序更改为阿拉伯语(
ar
),然后关闭并打开我的应用程序,显示布局方向显示为阿拉伯语,但字符串加载为英语

这是我在点击按钮时更改语言的代码

public static void setLocale(final Context ctx, final String lang) {
    Log.d(TAG, "##Changing Language to: " + lang);
    AppSettings.getInstance(ctx).save(PrefKeys.language, lang);
    final Locale loc = new Locale(lang);
 
    Resources resources = ctx.getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        configuration.setLocale(loc);
    else
        configuration.locale = loc;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
        ctx.createConfigurationContext(configuration);
     else
        resources.updateConfiguration(configuration, displayMetrics);
}
另外,在应用程序类上,我添加了应用该语言的代码,下面是应用程序类的代码

 @Override
public void onCreate() {
    super.onCreate();
    String lanuage = AppSettings.getInstance(getApplicationContext()).getLanguage();
    setLocale(new Locale(lanuage));
}


private void setLocale(Locale locale){
        Log.d(TAG,  "##Changing Language to: " + locale.getLanguage());
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            configuration.setLocale(locale);
        } else{
            configuration.locale=locale;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
            getApplicationContext().createConfigurationContext(configuration);
        } else {
            resources.updateConfiguration(configuration,displayMetrics);
        }
    }

创建一个基本活动并用您的活动扩展它,然后在基本活动上添加语言更改,怎么样

像下面这样

@SuppressLint("Registered")
open class BaseActivity : AppCompatActivity() {


companion object {
    var lang: String? = null
}


override fun attachBaseContext(base: Context?) {
    super.attachBaseContext(localeManager!!.setLocale(base!!))

}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // AndroidThreeTen.init(this)
  Utility.resetActivityTitle(this)
}

}

我创建了新的
BaseActivity.class
,并将该类扩展到主屏幕

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
    }

    protected abstract int getLayoutResourceId();


    @Override
    protected void attachBaseContext(Context newBase) {
        String language = AppSettings.getInstance(newBase).getLanguage();
        super.attachBaseContext(LocaleHelper.wrap(newBase, language));
    }


    private static class LocaleHelper extends ContextWrapper {

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

        public static ContextWrapper wrap(Context context, String language) {
            if (TextUtils.isEmpty(language.trim())) {
                return new LocaleHelper(context);
            }
            Configuration config = context.getResources().getConfiguration();
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(locale);
            } else {
                //noinspection deprecation
                config.locale = locale;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                config.setLayoutDirection(locale);
                context = context.createConfigurationContext(config);
            } else {
                //noinspection deprecation
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
            return new LocaleHelper(context);
        }

    } // LocaleHelper

}
这是对HomeActivity的代码修改

public class HomeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_home;
    }


}

创建一个基础活动并用您的活动扩展它,然后在基础上添加语言更改,怎么样activity@anwarsamir我没有试过it@anwarsamir当我改为BaseActivity时,它工作正常。谢谢你的建议不客气,请你对评论投赞成票并接受我的回答好吗