在Androidmanifest.xml文件中的 application 标签内加上 android:supportsRtl="true"属性,在设置为强制使用从右到左的布局文件 时,才会有效果。
设置android:supportsRtl="true"无效
今天解bug时,遇到这样一个问题:
问题描述:切换系统语言为阿拉伯文时,actionbar布局没有变为从右向左排列。
于是,我在Androidmanifest.xml文件中的 application 标签内加上 Android:supportsRtl="true"属性,发现没有起到效果。
原因在于 Androidmanifest.xml中对最小SDK版本和目标版本做了要求:
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="11" />
而使用 android:supportsRtl="true"要求最低SDK版本为17,再将上面那两句直接删除或者是修改为>=17的版本即可。
注意:
由于布局方向可以是从右到左的,所以在写xml布局的时候,为了防止出现布局混乱的现象,不要使用诸如layout_marginRight这种,而应该是layout_marginEnd这种
判断是否是中文:
private boolean isZh() {
Locale locale = getResources().getConfiguration().locale;String language = locale.getLanguage();
if (language.endsWith("zh")) {
Log.i("---", "-------zh");
return true;
} else {
Log.i("---", "-------en");
return false;
}
}
实现步骤:
学习阿拉伯等语言的由右至左布局属性:
1、在清单文件里application添加属性android:supportsRtl="true"
2、布局里有left必须有start,有right必须有end,或可以直接替换掉left和right
3、代码里获取当前语言种类并判断:String language = getResources().getConfiguration().locale.getLanguage();
布局:
(1)代码中:ll.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
(2)在布局中添加
android:layoutDirection="rtl"
EditText:et.setGravity(Gravity.RIGHT|Gravity.TOP);