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

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:paddingLeft
android:layout_marginLeft
android:paddingRight
android:layout_marginRight

to


android:paddingStart
android:layout_marginStart
android:paddingEnd
android: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 use
if (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());
}

// usageage
isTextRTL(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


QFQ
study&&sport&&fight
220 声望
1 粉丝
0 条评论
推荐阅读
ffmpeg-jni-example
jni的实现上使用直接传递ffmpeg command的方式,重写了ffmpeg.c的main()接口,只需要传入合适的ffmpeg命令即可使用

tainzhi1阅读 4.3k

程序员英语学习指南
动机为什么程序员要学习英语?工作:我们每天接触的代码都是英文的、包括很多技术文档也是英文的学习:最新最前沿的技术最开始都是只有English版本就业:学好英语让你的就业范围扩大到全球,而不只限于国内目标读...

九旬6阅读 612

安卓逆向之破解某成人APP播放次数限制
某成人水果APP非VIP用户存在播放次数限制,每天只能播放3次。超过3次需要购买VIP。 由于过于贫穷,于是抽空,对其安卓APP进行了逆向分析,最终成功破解了其播放次数限制。

悖论3阅读 1.2k评论 3

封面图
这一次,解决Flutter Dialog的各种痛点!
4.0版本做了重大调整,迁移请参照: SmartDialog 3.x 迁移 4.0本文内容已更新,文中内容及其代码皆为4.0用法前言Q:你一生中闻过最臭的东西,是什么?A:我那早已腐烂的梦。兄弟萌!!!我又来了!这次,我能自信...

小呆呆6661阅读 3.4k

封面图
uni-app中安卓包检查更新、新版本下载、下载进度条显示功能实现
如果想要做一个app的话,可以有很多种选择方案,uni-app是其中的一个性价比高一些(坑多一些)的方案。本文记录一下,uni-app打安卓包以后,需要检查并下载更新,且显示进度条的功能。

水冗水孚2阅读 721

Flutter 让你的Dialog脱胎换骨吧!(Attach,Dialog,Loading,Toast)
4.0版本做了重大调整,迁移请参照: SmartDialog 3.x 迁移 4.0本文内容已更新,文中内容及其代码皆为4.0用法前言Q:你一生中闻过最臭的东西,是什么?A:我那早已腐烂的梦。兄弟萌!!!我又来了!这次,我能自信...

小呆呆6662阅读 2.3k

封面图
2022 年终总结|致敬即将过去的,匆忙而又虐心的一年...
本文参与了 SegmentFault 思否年度征文「一名技术人的 2022」,欢迎正在阅读的你也加入。 时间,总是过的这么快,快得让人猝不及防,眨眼又是一年的末...似乎儿时的文章中,对于时间的流逝,描述的最多的便是:白...

贺biubiu1阅读 1.3k

封面图
220 声望
1 粉丝
宣传栏