当前位置

网站首页> 程序设计 > 开源项目 > 程序开发 > 浏览文章

Android RTL布局和双向字符集显示

作者:小梦 来源: 网络 时间: 2024-03-16 阅读:

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.

To take advantage of RTL layout mirroring, simply make the following changes to your app.

How to use

Declare in you app manifest that your app supports RTL mirroring

<application>    android:supportsRTL="true"</application>

Change all your app's "left/right" layout properties to new "start/end" equivalents

android:paddingLeftandroid:layout_marginLeftandroid:paddingRightandroid:layout_marginRight

to

android:paddingStartandroid:layout_marginStartandroid:paddingEndandroid:layout_marginEnd

Other APIs

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.

android:layoutDirection 

attribute for setting the direction of a component's text.

android:textDirection 

attribute for setting the alignment of a component's text.

android:textAlignment

method for getting the Locale-specified direction`

getLayoutDirectionFromLocale()

Whether to use

View.isLayoutRtl()

the base Class Viewhas the function isLayoutRtl() to judge the layout direction

// if this class is the subclass of View, then it can useif (isLayoutRtl()) {    }

use Configuration.getLayoutDirection()

import android.content.res.Configuration;Configuration config = getResources().getConfiguratin();if (config.getLayoutDirection() == View.LAYOUT_DIRECTIN_RTL) { }

Whether to use RTL by Locale

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());}// usageageisTextRTL(Locale.getDefault());

Create custome versions of layout, drawables and other resources fro display when a right-to-left is in use

res/  drawable-hdpi // Default  drawable-ldltr-hdpi // LTR  drawable-ldrtl-hpid // RTL

Support BiDi(双向字符集)显示

参考在 Java 开发过程中支持双向字符集语言(BiDi)

双向字符集(BiDi)通常是指文字可以从左到有(LTR)和从右到左(RTL)双向书写的文字.

比如本国文字是从右向左(乌尔都语, 阿拉伯语)与英语混排.

双向字符集语言处理算法

在 BiDi 中,所有的非标点符号被称为强字符。而标点符号既可以是从左向右 LTR 也可以是从右向左 RTL。因为不含任何的方向信息,所以被称为弱字符

  • 标点符号放在两段有相同方向文字的中间,标点符号将继承相同的方向

  • 标点符号放在两段有不同方向的文字中间,标点符号将继承全局方向

  • 一个弱字符紧挨着一个弱字符,BiDi 算法将根据最近相邻的“强”字符来决定字符方向。在某些情况下,这会导致非预期的情况出现。为了纠正错误,需要使用伪强字符RLM(\u200E)或者LRM(\u200F)插入到文字中间来调整字符的显示。

显然RLM, 表示从右到左, LRM从左到右. 使用时, 在要需要的的伪强字符后添加RLM或者LRM

References

[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

热点阅读

网友最爱