单个屏幕上多于一种语言的Android字体 [英] Android Font in more then one langauge on single screen
问题描述
我想改变阿拉伯文字体,如果我在整个应用程序中应用字体也会改变乌尔都语和英语的外观。所以,这将是很好的适用于FontFace每个包含阿拉伯语字体的每个文本的字体或有一些更好的方法来实现呢?
我也想知道如何删除自定义字体,以便通过代码重新获得默认字体。对不起,如果这个问题是幼稚的,我只是想知道有什么可能做到这一点。
例如,如果你想应用程序支持英文
和阿拉伯文字符串(除了默认字符串)
,您可以简单地创建两个额外的
资源目录,称为 / res / values-en
(英文strings.xml)和
/ res / values-ar
(阿拉伯语strings.xml) p>
在 strings.xml
文件中,
资源名称是相同的。
例如, /res/values-en/strings.xml
文件可能是
这个:
<?xml version =1.0encoding =utf-8?>
<资源>
< string name =hello>英文版Hello!< / string>
< /资源>
/res/values-ar/strings.xml文件看起来像这样: p>
<?xml version =1.0encoding =utf-8?>
<资源>
< string name =hello>مرحبافياللغةالإنجليزية< / string>
< /资源>
同样,/res/values-ur_IN/strings.xml文件对于urdu来说应该是这样的:
ur_IN for印度ur_PK for pakisthan
<?xml version =1.0encoding =utf-8?>
<资源>
<字符串名称=hello>英文字母!< /字符串>
< /资源>
显示字符串的/ res / layout目录中的默认布局文件是指
字符串由变量名@ string / hello,而不考虑使用哪种语言或目录
字符串资源所在。
Android操作系统决定哪个版本
在运行时加载的字符串(法文,英文或默认)。用TextView控件
显示字符串的布局可能如下所示:
<?xml version =1.0encoding =utf-8?>
< LinearLayout
xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =vertical
android:layout_width =fill_parent
android:layout_height =fill_parent>
< TextView
android:layout_width =fill_parent
android:layout_height =fill_parent
android:text =@ string / hello>
< / LinearLayout>
字符串以正常方式以编程方式访问:
String str = getString(R.string.hello);
要改变语言,你需要改变语言。
btn_english.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
locale locale = new Locale(en);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext ().getResources()。updateConfiguration(config,getBaseContext()。getResources()。getDisplayMetrics());
Toast.makeText(this,getResources()。getString(R.string.lbl_langSelectEnglis),Toast.LENGTH_SHORT ).show();
}
});
$ b btn_arbice.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
locale locale = new Locale(ar);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext ().getResources()。updateConfiguration(config,getBaseContext()。getResources()。getDisplayMetrics());
Toast.makeText(this,getResources()。getString(R.string.lbl_langSelecURdu),Toast.LENGTH_SHORT ).show();
}
});
$ b btn_urdu.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Locale locale = new Locale(ur_IN);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale; $ b $ getBaseContext()。getResources ().updateConfiguration(config,getBaseContext()。getResources()。getDisplayMetrics());
Toast.makeText(HomeActivity.this,getResources().getString(R.string.lbl_langSelectEnglis),Toast.LENGTH_SHORT)。 show();
}
});
另请参阅此示例中的单个屏幕
字符串段=怎么了ضعيف;
int NO_FLAG = 0;
双向双向=新双向(段落,NO_FLAG);
int runCount = bidi.getRunCount();
for(int i = 0; i< runCount; i ++){
String ltrtl = bidi.getRunLevel(i)%2 == 0? ltr:rtl;
String subString = paragraph.substring(bidi.getRunStart(i),bidi.getRunLimit(i));
Log.d(>> bidi:+ i,subString +is+ ltrtl);
打印:
嘿,这是怎么了ltr
ضعيفis rtl
所以现在可以很容易地建立 TypefaceSpan
或 MetricAffectingSpan
基于语言方向:
SpannableString spanString = new SpannableString(paragraph);
for(int i = 0; i< runCount; i ++){
Object span = bidi.getRunLevel(i)%2 == 0? ltrFontSpan:rtlFontSpan;
spanString.setSpan(span,bidi.getRunStart(i),bidi.getRunLimit(i),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(spanString);
I am new to android. And i am implementing an application which will be in two langauge english and urdu. Basically there will be some text in arabic and its meaning will be in english or urdu based on the user selected language.
I want to change the font of arabic text, If i apply font in whole application it will change the look of urdu and english also. So is this will be fine to just apply font to each textview that contains arabic fonts using TypeFace or there is some better way to achieve this?
And also i want to know how to remove custom fonts in order to get default font back again through code. Sorry if this question is childish i just want to know what are the possibilities to do this.
for example if you want your application to support both English
and Arabic strings (in addition to the default strings),
you can simply create two additional
resource directories called /res/values-en
(for the English strings.xml) and
/res/values-ar
(for the Arabic strings.xml).
Within the strings.xml
files, the
resource names are the same.
For example, the /res/values-en/strings.xml
file could
look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
Whereas, the /res/values-ar/strings.xml file would look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">مرحبا في اللغة الإنجليزية</string>
</resources>
also , the /res/values-ur_IN/strings.xml file would look like this for urdu:
ur_IN for india ur_PK for pakisthan
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">انگریزی میں خوش!!</string>
</resources>
A default layout file in the /res/layout directory that displays the string refers to the string by the variable name @string/hello, without regard to which language or directory the string resource is in.
The Android operating system determines which version of the string (French, English, or default) to load at runtime.A layout with a TextView control to display the string might look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" >
</LinearLayout>
The string is accessed programmatically in the normal way:
String str = getString(R.string.hello);
For change the language you need to like that change lang..
btn_english.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
}
});
btn_arbice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, getResources().getString(R.string.lbl_langSelecURdu), Toast.LENGTH_SHORT).show();
}
});
btn_urdu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ur_IN");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(HomeActivity.this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
}
});
Also see this example for single screen
Custom fonts for TextView based on languages inside String
String paragraph = "hey what's up ضعيف"; int NO_FLAG = 0; Bidi bidi = new Bidi(paragraph, NO_FLAG); int runCount = bidi.getRunCount(); for (int i = 0; i < runCount; i++) { String ltrtl = bidi.getRunLevel(i) % 2 == 0 ? "ltr" : "rtl"; String subString = paragraph.substring(bidi.getRunStart(i), bidi.getRunLimit(i)); Log.d(">>bidi:" + i, subString+" is "+ltrtl); }
prints:
hey what's up is ltr
ضعيف is rtl
So now one can easily build TypefaceSpan
or MetricAffectingSpan
based on language direction like this:
SpannableString spanString = new SpannableString(paragraph);
for (int i = 0; i < runCount; i++) {
Object span = bidi.getRunLevel(i) % 2 == 0 ? ltrFontSpan : rtlFontSpan;
spanString.setSpan(span, bidi.getRunStart(i), bidi.getRunLimit(i), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(spanString);
这篇关于单个屏幕上多于一种语言的Android字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!