我有一个用 XML 定义的 TextView 小部件,但我想以在运行时确定的语言以编程方式设置特定字符串。我尝试使用以下代码片段,但它不起作用。它显示一个空白文本。只有默认的英文文本才能正常工作。
PS:我假设这些语言字体已经可用,因为当我用这些语言在 XML 资源文件中硬编码静态字符串时,它会正确显示。
tv = (TextView) row.findViewById(R.id.myTextWid);
if (condition1) {
Locale loc = new Locale("ta", "IN");
tv.setTextLocale(loc);
tv.setText("என் தமிழ் உரை"); // my tamil text
} else if (condition2) {
Locale loc = new Locale("hi", "IN");
tv.setTextLocale(loc);
tv.setText("मेरी हिंदी पाठ"); // my hindi text
} else {
tv.setText("My default english text");
}
使用 Android Studio 1.0.1;使用 API 21 编译;最小 SDK API 15