我要求以多种语言显示一个文本,特别是Kannada和Telugu 我正在开发最低要求的API 14(4.0)
谢谢你
答案 0 :(得分:22)
试试这个......
我在这里分享了整个应用程序代码。
项目结构
<强> activity_main_activity1.xml 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2b579a"
android:orientation="vertical"
tools:context=".MainActivity1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/kannada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="start"
android:text="@string/kannada"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<Button
android:id="@+id/telugu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="end"
android:text="@string/telugu"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<Button
android:id="@+id/english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="end"
android:text="@string/english"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/note"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</LinearLayout>
<强>值/ strings.xml中强>
<强>值-KN / strings.xml中强>
kannada语言的字符串。
<强>值-TE / strings.xml中强>
设置字体
下载字体here。
<强> MainActivity1.java 强>
package com.hirecraft.stackoverflowtest;
import java.util.Locale;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity1 extends Activity {
/**
* Declaration
*/
Button kannada, telugu, english;
String currentLanguage;
TextView news;
Typeface kannadaFont, teluguFont;
/**
* This class describes all device configuration information
* that can impact the resources the application retrieves. This
* includes both user-specified configuration options (locale
* and scaling) as well as device configurations (such as input
* modes, screen size and screen orientation).
*/
Configuration config;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
/**
* Initialization
*/
currentLanguage = "";
kannada = (Button) findViewById(R.id.kannada);
telugu = (Button) findViewById(R.id.telugu);
english = (Button) findViewById(R.id.english);
news = (TextView) findViewById(R.id.news);
/**
* Initialize the fonts.
*/
kannadaFont = Typeface.createFromAsset(getAssets(), "fonts/akshar.ttf");
teluguFont = Typeface.createFromAsset(getAssets(), "fonts/gautami.ttf");
/**
* Event for Kannada
*/
kannada.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* "kn" is the localization code for our Kannada language.
*/
currentLanguage = "kn";
Locale locale = new Locale(currentLanguage);
Locale.setDefault(locale);
/**
* Print the current language
*/
System.out.println("My current language: "
+ Locale.getDefault());
config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
news.setText(R.string.note);
news.setTypeface(kannadaFont);
}
});
telugu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* "te" is the localization code for our Telugu language.
*/
currentLanguage = "te";
Locale locale = new Locale(currentLanguage);
Locale.setDefault(locale);
/**
* Print the current language
*/
System.out.println("My current language: "
+ Locale.getDefault());
config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
news.setText(R.string.note);
news.setTypeface(teluguFont);
}
});
english.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* "en" is the localization code for our default English language.
*/
currentLanguage = "en";
Locale locale = new Locale(currentLanguage);
Locale.setDefault(locale);
/**
* Print the current language
*/
System.out.println("My current language: "
+ Locale.getDefault());
config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
news.setText(R.string.note);
}
});
}
}
<强>的AndroidManifest.xml 强>
截屏:
<强> 1。默认语言环境(英语)
<强> 2。埃纳德语强>
第3。泰卢固强>
快乐的编码.....
答案 1 :(得分:1)
你需要两件事: -
如下: -
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
更多detail阅读
答案 2 :(得分:0)
使用android的内置Locale包来获取可用的语言环境。然后过滤印度语言,最后以相应的字体显示它们,使用locale.getDisplayLanguage(locale)
,如下所示:
for(locale in Locale.getAvailableLocales())
if ("IN" in locale.country)
availableLanguages.add(Pair(locale.language,locale.getDisplayLanguage(locale)))