Android 4.1(Jelly Bean) introduced limited support for bidirectional text in TextView and EditText elements, allowing apps to display and edit text int both left-to-right(LTR) and right-to-left(RTL) scripts. Android 4.2 added full native support for RTL layouts, including layout mirroring, allowing you to deliver the same great app experience to all your users, whether their language uses a script that reads right-to-left or one that reads left-to-right.html
To take advantage of RTL layout mirroring, simply make the following changes to your app.java
<application> android:supportsRTL="true" </application>
android:paddingLeft android:layout_marginLeft android:paddingRight android:layout_marginRight
toandroid
android:paddingStart android:layout_marginStart android:paddingEnd android:layout_marginEnd
For more precise control over your app UI in both LTR and RTL mode, Android 4.2 includes the following new APIs to help manage View components:算法
attribute for setting the direction of a component's layout.app
android:layoutDirection
attribute for setting the direction of a component's text.ui
android:textDirection
attribute for setting the alignment of a component's text.this
android:textAlignment
method for getting the Locale-specified direction`.net
getLayoutDirectionFromLocale()
the base Class View
has the function isLayoutRtl()
to judge the layout directioncode
// if this class is the subclass of View, then it can use if (isLayoutRtl()) { }
import android.content.res.Configuration; Configuration config = getResources().getConfiguratin(); if (config.getLayoutDirection() == View.LAYOUT_DIRECTIN_RTL) { }
import java.util.Collections; import java.util.HashSet; import java.util.Set; private static final Set<String> sRTL; static { Set<String> lang = new HashSet<String>(); private static final Set<String> sRTL; } static { Set<String> lang = new HashSet<String>(); lang.add("ar"); lang.add("dv"); lang.add("fa"); lang.add("ha"); lang.add("he"); lang.add("iw"); lang.add("ji"); lang.add("ps"); lang.add("ur"); lang.add("yi"); sRTL = Collections.unmodifiableSet(lang); } public static boolean isTextRTL(Locale locale) { return sRTL.contains(locale.getLanguage()); } // usageage isTextRTL(Locale.getDefault());
res/ drawable-hdpi // Default drawable-ldltr-hdpi // LTR drawable-ldrtl-hpid // RTL
參考在 Java 開發過程當中支持雙向字符集語言(BiDi)component
雙向字符集(BiDi)一般是指文字能夠從左到有(LTR)和從右到左(RTL)雙向書寫的文字.
好比本國文字是從右向左(烏爾都語, 阿拉伯語)與英語混排.
在 BiDi 中,全部的非標點符號被稱爲強字符。而標點符號既能夠是從左向右 LTR 也能夠是從右向左 RTL。由於不含任何的方向信息,因此被稱爲弱字符。
標點符號放在兩段有相同方向文字的中間,標點符號將繼承相同的方向
標點符號放在兩段有不一樣方向的文字中間,標點符號將繼承全局方向
一個弱字符緊挨着一個弱字符,BiDi 算法將根據最近相鄰的「強」字符來決定字符方向。在某些狀況下,這會致使非預期的狀況出現。爲了糾正錯誤,須要使用僞強字符RLM(\u200E
)或者LRM(\u200F
)插入到文字中間來調整字符的顯示。
顯然RLM, 表示從右到左, LRM從左到右. 使用時, 在要須要的的僞強字符後添加RLM或者LRM
[1] http://blog.csdn.net/thinkinwm/article/details/21108601
[2] http://blog.csdn.net/ultrapro/article/details/8690145
[3] http://stackoverflow.com/questions/20814564/how-to-find-out-locale-dependent-text-orientation-in-java#20821395
[4] http://article.yeeyan.org/view/37503/335086