字符串文件: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中使用国家/地区代码以编程方式更改语言相关推荐

  1. java地区_在Java中存储国家/地区代码,名称和Continent的最佳方式

    在ISO 3166中有246个国家,你可能会在此背后得到一个继电器大枚举.我更喜欢使用包含国家/地区列表的 XML文件,您可以从 http://www.iso.org/下载一个并加载它们(例如,当应用 ...

  2. android语言 选择题,Android N以编程方式更改语言

    首先,你应该知道在25个API Resources.updateConfiguration(...)中已弃用.因此,你可以执行以下操作: 1)你需要创建自己的ContextWrapper,它将覆盖ba ...

  3. wxpython中表格顶角怎么设置_当wxGrid中的某个单元格以编程方式更改时,突出显示该行中的一行(使用wxPython)...

    您做的是正确的,唯一的问题是您可能没有在GridTableBase更新之后手动刷新网格.这里有一个小的工作例子,希望能帮到你.在import wx, wx.grid class GridData(wx ...

  4. android收藏功能demo,Android使用Realm数据库实现App中的收藏功能(代码详解)

    前 言 App数据持久化功能是每个App必不可少的功能,而Android最常用的数据持久化方式主要有以下的五种方式: 使用SharedPreferences存储数据: 文件存储数据: SQLite数据 ...

  5. android设置ap密码,Android AP Wifi - 以编程方式更改网络共享密码(setWifiApConfiguration)...

    我正在尝试以编程方式更改Android AP Wifi密码,但不幸的是停止了工作. 使用Android 4.2工作正常,但4.4 Kitkat已停止. 我通过反射使用setWifiApConfigur ...

  6. java menuitem 图标_java – 如何以编程方式更改ActionBar中的MenuItem图标

    如何以编程方式更改ActionBar中的MenuItem图标?我试着用 MenuItem menuItem = (MenuItem)findViewById(R.id.action_settings) ...

  7. powershell 编程_如何使用PowerShell以编程方式更改Visual Studio中的默认浏览器,并可能使自己陷入困境...

    powershell 编程 UPDATE: Why my own MacGyver solution was brilliant in its horrible way, the folks over ...

  8. android 是什么编码格式,在Android的JPG图像编码和解码

    我正在Android上创建一个应用程序,我需要操纵我的JPG文件.我没有获得JPG格式的标题信息,因此我将它转换为位图,处理位图中的像素值,然后再将其转换回JPG.在Android的JPG图像编码和解 ...

  9. android开机图片修改工具栏,如何以编程方式更改android工具栏中的图标...

    执行此操作的正确方法是覆盖Activity中的onPrepareOptionsMenu方法. 假设您有一个应用程序,用户可以在其中"收藏"或"取消收藏"项目.首 ...

最新文章

  1. 4-1 图像特效介绍
  2. 两幅相同大小图像的相似程度的两个评价指标-PSNR和SSIM
  3. mysql时间日期操作
  4. search by Belonging to my team测试 - with manager role
  5. 【Spark】Spark SQL 物化视图技术原理与实践
  6. [POJ3096]Surprising Strings
  7. Windows下命令模式安装mysql
  8. linux ip被占用顶掉,记一次 Linux服务器被***后的排查思路
  9. 2022-03微软漏洞通告
  10. 【C#进阶3-7】C# Socket通讯
  11. 【hadoop生态之Hive】Hive的DDL数据定义语言【笔记+代码】
  12. 解密回声消除技术之二(应用篇)
  13. 为了广大AI男的婚姻幸福,求求AI男们不要学习金融男好榜样了
  14. 讯飞智能录音笔SR502内存升级,实力更强大
  15. vue 3.0 使用ref获取dom元素
  16. linux如何修改用户的密码
  17. HTML-网页-3D旋转相册-创意相册
  18. 【2022保研经验帖】夏令营/九推/清北/浙大/南大/北理/北邮/中科院
  19. java.lang.Integer connot be cast to class java.lang.String
  20. 传说中的死机短信,不知是真是假

热门文章

  1. 折纸机器人的步骤图解_折纸图解走路的人
  2. js svg语音波动动画_让动效更酷炫!4 个常见且常用的 SVG 交互动画方法
  3. 有向图的广度优先遍历_图的两种遍历方式
  4. pip install 镜像_pypi私有镜像仓库部署
  5. windows 下anaconda创建环境慢的解决办法
  6. 新一代爬虫利器 —— Playwright
  7. 好习惯!pandas 8 个常用的 index 设置
  8. 用Python爬取Bilibili上二次元妹子的视频
  9. 大公司病,真 TM 形象
  10. 找出两列表的共有元素python_python 找出两个dataframe中不同的元素