Origin of RTL language

RTL stands for right-to-left. It means that people’s writing and reading habits continue from right to left and left. Common RTL languages include Arabic and Hebrew.

Take a look at the LTR(left) layout compared to the RTL(right) layout:

  

Is there any support for this right-to-left habit in the Android layout?

The answer is yes: starting with Android 4.2, SDK 17, there is full native layout support, allowing for mirrored layouts, and support for both RTL and LTR.

Next I will show you how to adapt Arabic step by step.

attribute

name desc chinese
android:layoutDirection attribute for setting the direction of a component’s layout Sets the layout orientation of components
android:textDirection attribute for setting the direction of a component’s text Sets the text orientation of the component
android:textAlignment attribute for setting the alignment of a component’s text Sets the alignment of text
getLayoutDirectionFromLocale() method for getting the Locale-specified direction Gets the customary layout of the specified locale

The premise condition

Add right-to-left layout code to the application node in the androidmanifest.xml file

 <application
        ...
        android:supportsRtl="true" >
        ...
</application>
Copy the code

To switch between languages

Change language-magazine-in-Android = what-is-the-list-of-supported-languages-locales-on-Android

Resources res = getResources(); 
DisplayMetrics dm = res.getDisplayMetrics();
Locale locale = new Locale(languageToLoad); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
res.updateConfiguration(conf, dm); 
Copy the code

AS one-key adaptation

AS supports one-click adaptation of RTL. It is mainly used to add Start and End attributes to the Left and Right attributes of the original Layout. You can’t be lazy when you write the layout, you still have to add.)

The Start attribute corresponds to Left in LTR and Right in RTL, and is supported starting in API 17. In order to be compatible with lower versions, both Left and Start are required. From the point of view of the market, there are not many mobile phone users under Android 4.2 system. My suggestion is whether they can be compatible or not. The specific users of your products under Android 4.2 system need to be determined.

Refactor > Add RTL Support Where Possible…

Use third-party plug-ins

The name of the describe
gradle-android-rtl Automatically fixes labels in layout files that do not add RTL support

Comparison with the AS plug-in:

  • Better performance. When dealing with large volume file changes, the AS tool can cause delays
  • Padding tag completion is supported

Use global styles

EditText

Set-a-consistent -style-to-all- EditText-for-e-g

android:textAlignment="viewStart"
android:gravity="start"
Copy the code

Then we can set all the EditText in the style.xml style

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       ...
       <item name="editTextStyle">@style/EditTextStyle.Alignment</item>
       ...
</style>

<style name="EditTextStyle.Alignment" parent="@android:style/Widget.EditText">
        <item name="android:textAlignment">viewStart</item>
        <item name="android:gravity">start</item>
        <item name="android:textDirection">locale</item>
</style>
Copy the code

TextView

Globally add a link to all textViews related to RTL properties :setting-global-styles-for-views-in-android

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       ...
       <item name="android:textViewStyle">@style/TextViewStyle.TextDirection</item>
       ...
</style>

<style name="TextViewStyle.TextDirection" parent="android:Widget.TextView">
        <item name="android:textDirection">locale</item>
</style>
Copy the code

Check if it is RTL layout

TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL
Copy the code

Process the collection in reverse order

This can be useful in some situations

Collections.reverse(List<? > list);Copy the code

Code to dynamically set the setMargins control

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 10, 0);
params.setMarginEnd(10);
Copy the code

ViewPager

Tabs -swipe-direction-in-right-to-left- Android -app

Most Android controls support RTL, except ViewPager. GitHub has modified ViewPager to support RTL

Adaptation to summarize

  1. You can use FrameLayout for horizontal LinearLayout. You can use Layout_gravity to set the corresponding properties if the control needs to be left or right
  2. When switching Arabic, there will be an increase in the distance between grid layout items. The solution is as follows: The grid line ItemDecoration needs to be judged by adding language, and the original left and right margins can be changed
  3. Disallow previous sideslip returns to avoid conflict
  4. For some direction ICONS, re-create a relative direction and put it in mipmap-lDRtl-xxxhdpi package
  5. Animation flip, put the corresponding animation in anim-LDRTL for reverse processing
  6. If you set paddingLeft, drawableLeft, and so on in your layout, you change that to a rTL-enabled paddingStart, drawableStart; However, there are some areas that can be omitted, such as the quantity badge on the shopping cart, which feels strange after being added, so I will not add it
  7. Use the language switch tool in the preview layout tool on the right of AS to switch to Arabic and see the layout effect in real time
  8. Add Android :layoutDirection=”locale” to the EditText. If you have TextInputLayout outside, set android:layoutDirection=”locale” to the EditText. If you type in the password you need to add a property android:textAlignment=”viewStart”
  9. TextView needs android:textAlignment=”viewStart or viewEnd” and Android :textDirection=”locale”
  10. Consider using StaggeredGridLayoutManager RecyclerView network layout, if the quantity is too much grid layout is not recommended, sliding chaos may occur
  11. %d = %d; %d = %d; % D = % D; % D = % D; % D = % D

Suggested plan

  1. Start with the basic class and determine if it is In Arabic. If so, set the interface to display from right to left
  2. Adaptation by module
  3. Complex modules can be placed in the layout-ldrtl package to create a separate layout for Arabic, such as the details page

The resources

  1. Android-developers.googleblog.com/2013/03/nat…
  2. medium.com/@zhangqichu…
  3. Mobikul.com/just-few-st…
  4. Blog.robustastudio.com/featured/an…
  5. Blog.csdn.net/figo0423/ar…
  6. Blog.csdn.net/wxx614817/a…
  7. Jiajixin. Cn / 2016/10/08 /…
  8. www.apkbus.com/blog-327085…
  9. Droidyue.com/blog/2014/0…
  10. Chuansong. Me/n / 920084451…