我可以在textView中添加多语言吗?

问题描述 投票:0回答:2

我可以在textView中添加多语言吗?例如,我可以使用带有阿拉伯语文本和英文数字的textView吗?

android textview typeface
2个回答
1
投票

只要两个文本都以Unicode格式编码,Murillo Henrique建议的解决方案就可以使用。

另一方面,当任何一个文本都是非Unicode时,我们将无法在单个TextView控件中显示它。

enter image description here

如上所示,第一个TextView显示英语和阿拉伯语文本。这是因为两者都是Unicode文本。为此,可以简单地组合不同语言的字符串,如下所示。

txtUnicode.setText( "The Saved Group " + "الفرقة الناجية");

在第二个TextView控件中,加载的文本是非Unicode Malayalam文本。要正确显示非Unicode文本,需要加载相应的字体,如下所示。

Typeface tfMalayalam = Typeface.createFromAsset(getAssets(), "fonts/FML-TT-Leela_Heavy.ttf");
textNonUnicode.setTypeface(tfMalayalam);
textNonUnicode.setText("hnPbn¨I£n");

可以看出,传递给setText方法的文本是不可读的,因为它是非Unicode编码形式。但是在运行时提供正确的字体时,它会正确显示文本。

当我们处理来自遗留源的非Unicode文本时,这种非Unicode字体加载将非常有用。

在第三个TextView控件中,Unicode文本将附加到非Unicode文本,如下所示。

TextView textUnicodePlusNonUnicode = (TextView) findViewById(R.id.txtUnicdePlusNonUnicode);
textUnicodePlusNonUnicode.setTypeface(tfMalayalam);
textUnicodePlusNonUnicode.setText("hnPbn¨I£n" + " Unicode English");

因为设置了非Unicode字体,系统将尝试使用加载的字体指定的非Unicode编码方案显示Unicode文本。因此,提供的Unicode文本Unicode English将不会按预期显示。

作为附加信息,Google正在为Android平台定义名为Noto的通用字体。有关详细信息,请参阅this


0
投票

你有2个选择:

一:键入文本,但在键入期间在键盘中切换各种语言。

或者我推荐的第二个,因为你将所有文本放在一个文件中,以后更容易修改它。

myTextView.setText(R.string.text1 + " " + R.string.text2);

在String.xml中:

<string name="text1">Your english text goes here/>
<string name="text2">Your arabic text goes here/>
© www.soinside.com 2019 - 2024. All rights reserved.