日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android多语言编码格式,在Android中使用国家/地区代码以编程方式更改语言

發布時間:2024/9/3 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android多语言编码格式,在Android中使用国家/地区代码以编程方式更改语言 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串文件:values / string.xml和values-pt-rBr / string.xml

setLocale(new Locale("en"));

String eng = getString(R.string.hello_world);

setLocale(new Locale("pt", "Br"));

String bra = getString(R.string.hello_world);

if (!eng.equals(bra)) {

Log.i("locale_test", "it works!");

}

public void setLocale(final Locale locale) {

Resources res = getResources();

DisplayMetrics dm = res.getDisplayMetrics();

Locale.setDefault(locale);

android.content.res.Configuration conf = res.getConfiguration();

conf.locale = locale;

res.updateConfiguration(conf, dm);

}

更新:

從android開始N需要新方法

創建ContextWrapper類,并在您活動的attachBaseContext(Context)方法中使用它

public class ContextWrapper extends android.content.ContextWrapper {

public ContextWrapper(final 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);

} else {

configuration.setLocale(newLocale);

DisplayMetrics dm = res.getDisplayMetrics();

res.updateConfiguration(configuration, dm);

}

configuration.setLayoutDirection(newLocale);

context = context.createConfigurationContext(configuration);

return new ContextWrapper(context);

}

}

@Override protected void attachBaseContext(final Context newBase) {

String savedLocale = LocaleUtils.getSavedLocale();

if (isNotEmpty(savedLocale)) {

Locale appLocale = new Locale(savedLocale);

ContextWrapper wrapped = ContextWrapper.wrap(newBase, appLocale);

SalonyFormatter.getInstance(wrapped).refreshResources(wrapped);

super.attachBaseContext(wrapped);

} else {

super.attachBaseContext(newBase);

}

}

總結

以上是生活随笔為你收集整理的android多语言编码格式,在Android中使用国家/地区代码以编程方式更改语言的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。