如何判断android中的当前系统语言?

时间:2015-05-26 09:57:16

标签: java android locale

我在这里有一些问题。我只是想知道,如何为当前使用的系统语言制作if else语句?我有一个项目,我需要在系统语言改变时使用不同的字体。所以我的猜测是通过创建if else语句来解决这个问题。我的代码如下:

final Typeface en = Typeface.createFromAsset(getAssets(), "LCfont-en.ttf");
final Typeface ar = Typeface.createFromAsset(getAssets(), "LCfont-ar.ttf");
final Typeface cn = Typeface.createFromAsset(getAssets(), "LCfont-cn.ttf");
final Typeface tw = Typeface.createFromAsset(getAssets(), "LCfont-tw.ttf");
final Typeface th = Typeface.createFromAsset(getAssets(), "LCfont-th.ttf");

TextView text1 = (TextView) view.findViewById(R.id.TV);

text1.setTypeface(en);
text1.setTypeface(ar);
text1.setTypeface(cn);
text1.setTypeface(tw);
text1.setTypeface(th);

以上是代码,我想在系统语言改变时使用不同的字体。任何帮助非常感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以像这样使用Locale类来检索当前区域设置:

String localeCode = Locale.getDefault().getLanguage();
if ("en".equals(localCode)) {
    // ...
} else if ("es".equals(localCode)) {
    // ...
}

<强>替代

如果你只有少量的字体。您还可以在资源中定义资产字体名称,并通过配置覆盖它。 例如,在您的values / strings.xml中:

<string name="typeface_path">LCfont-en.ttf</string>

并在values-ar / strings.xml中:

<string name="typeface_path">LCfont-ar.ttf</string>

和其他语言的等价物。然后在您的代码中,您可以使用:

final Typeface typeface = Typeface.createFromAsset(getAssets(), getString(R.string.typeface_path));
TextView text1 = (TextView) view.findViewById(R.id.TV);
text1.setTypeface(typeface);