Android now offers several APIs that allow you to build user interfaces that gracefully transform layout orientation to support languages that use right-to-left (RTL) UIs and reading direction, such as Arabic and Hebrew.
To begin supporting RTL layouts in your app, set the android:supportsRtl attribute to the <application> element in your manifest file and set it “true". Once you enable this, the system will enable various RTL APIs to display your app with RTL layouts. For instance, the action bar will show the icon and title on the right side and action buttons on the left, and any layouts you’ve created with the framework-provided View classes will also be reversed.
If you need to further optimize the appearance of your app when displayed with an RTL layout, there are two basic levels of optimization:
- Convert left- and right-oriented layout properties to start- and end-oriented layout properties.
For example, use
android:layout_marginStartin place ofandroid:layout_marginLeftandandroid:layout_marginEndin place ofandroid:layout_marginRight.The
RelativeLayoutclass also provides the corresponding layout attributes to replace left/right positions, such asandroid:layout_alignParentStartto replaceandroid:layout_alignParentLeftandandroid:layout_toStartOfinstead ofandroid:layout_toLeftOf. - Or to provide complete optimization for RTL layouts, you can provide entirely separate layout files using the
ldrtlresource qualifier (ldrtlstands for layout-direction-right-to-left}). For example, you can save your default layout files inres/layout/and your RTL optimized layouts inres/layout-ldrtl/.The
ldrtlqualifier is great for drawable resources, so that you can provide graphics that are oriented in the direction corresponding to the reading direction.
Various other APIs are available across the framework to support RTL layouts, such as in the View class so that you can implement the proper behaviors for custom views and in Configuration to query the current layout direction.
Note: If you are using SQlite and have tables or column names that are “number only," be careful: usingString.format(String, Object...) can lead to errors where the numbers have been converted to their Arabic equivalents if your device has been set to the Arabic locale. You must use String.format(Locale,String,Object...) to ensure numbers are preserved as ASCII. Also use String.format("%d", int) instead of using String.valueOf(int) for formatting numbers.
本文介绍如何在Android应用中支持从右至左(RTL)的布局方向,以适应如阿拉伯语和希伯来语等语言环境。文章详细说明了启用RTL支持的方法,并提供了两种优化策略:一是使用start和end属性替代left和right属性;二是为RTL布局提供独立的资源文件。
4839

被折叠的 条评论
为什么被折叠?



