Android - 如何在没有本地化的情况下选择 strings.xml [英] Android - How can i select a strings.xml without Localization

查看:34
本文介绍了Android - 如何在没有本地化的情况下选择 strings.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不更改设备语言的情况下手动选择 strings.xml 文件?

Is it possible to manually select a strings.xml file without changing the language of your device?

我想让 Button 在两种语言之间切换,但我发现的只是使用 android studio 的内置功能来创建不同的 strings.xml,由您的本地化选择.

I would like to have Button to switch between two languages, but all i found is to use the build-in feature of android studio to create different strings.xml, which are selected by your localization.

推荐答案

如果你想使用不同的 strings.xml 你必须切换本地化.这可以通过编程方式完成,但强烈不建议这样做,因为您会弄乱系统.

If you want to use a different strings.xml you have to switch localization. This can be done programmatically but is highly not recommended because you will mess with the system.

你可以创建一个不同的资源 XML 文件,你可以把你的字符串放在那里,给它们一个唯一的 id 并以相同的方式获取它们 (R.id.your_string)

You can create a different resource XML file that you can put your strings there, give them a unique id and fetch them in the same way (R.id.your_string)

您创建一个放入/res/values 的 XML 文件.该文件将使用与 strings.xml 相同的方式声明一些字符串.带有 id、值和所有内容.然后从代码中你所做的是有条件地改变字符串.例如:

You create an XML file that you put into /res/values. This file will declare some strings in the same way the strings.xml does. With ids, values and everything. Then from the code what you do is to conditionally change strings. For example :

如果(这个){//从strings.xml中获取字符串(R.strings.your_string别的 {//从 your_file.xml 中获取字符串,方法与上面相同.{

if (this){ //get the string from strings.xml here (R.strings.your_string else { //get the string from your_file.xml here the same way you did above. {

有关详细信息,请查看文档.

For more info check the documentation.

这篇关于Android - 如何在没有本地化的情况下选择 strings.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆