有没有办法自动设置所有布局,以支持Android中的RTL和LTR方向类型的语言 [英] Is there any way to automatically set the all layouts to support for both RTL and LTR direction types of languages in android
问题描述
我是android开发的新手,坚持改变整个 android
应用程序的语言。我找到了一种方法将字符串
设置为 strings-languagecode
in values
目录和 android:supportsRtl =true
在清单文件中,它更适合更改文本字段的值
和他们的方向 LTR
和 RTL
自动但我在 EditText中遇到问题
字段,因为它的方向需要由每个 xml
文件的Java代码更改和设置。有没有办法在整个应用程序中以最小的努力自动设置 EditText
的方向?请注意我的项目中最低 api等级15
。请帮帮我。
I am new in android development, stuck to change the language of the whole android
application. i found a way to set the strings
into strings-languagecode
in values
directory and android:supportsRtl="true"
in manifest file, it is good for changing the value of the text fields
and their direction LTR
and RTL
automatically but I am getting the problem in EditText
field because it's direction needs to be changed and set by the Java code for every xml
file. Is there any way to set the direction of the EditText
automatically in whole application with minimum effort? Please note I have minimum api level 15
in my project. Please help me out.
提前致谢。
推荐答案
你可以尝试这些方法来改变RTL或LTR布局方向。
You can try these methods to change RTL or LTR layout directions.
对于左转:
ex。 locale:Arabic(ar),Hebrew(he);
ex. locale : Arabic(ar), Hebrew(he);
private void setRtl(){
String languageToLoad = "ar"; // rtl language Arabic
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
//layout direction
Bidi b = new Bidi(languageToLoad,Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
b.isRightToLeft();
//save current locale in SharedPreferences
SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();
startActivity(...);// refresh activity.
}
左转:
ex locale:English(en),Tamil(ta)。等。
ex locale : English(en), Tamil(ta)., etc.
private void setLtr(){
String languageToLoad = "en"; // ltr language English
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
//layout direction
Bidi b = new Bidi(languageToLoad, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
b.isLeftToRight();
//save current locale in SharedPreferences
SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();
startActivity(...);// refresh activity.
}
调用这些方法进行更改RTL或LTR布局方向。
Call these method to change RTL or LTR layout directions.
快乐编码....
这篇关于有没有办法自动设置所有布局,以支持Android中的RTL和LTR方向类型的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!