Identifying RTL language in Android

前端 未结 16 1743
既然无缘
既然无缘 2020-11-28 06:59

Is there a way to identify RTL (right-to-left) language, apart from testing language code against all RTL languages?

Since API 17+ allows several resources for RTL a

16条回答
  •  生来不讨喜
    2020-11-28 07:30

    Just use this code:

     public static boolean isRTL() {
       return isRTL(Locale.getDefault());
     }
    
     public static boolean isRTL(Locale locale) {
      final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
      return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
           directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
     }
    
     if (isRTL()) {
       // The view has RTL layout
     }
     else {
       // The view has LTR layout
     }
    

    This will work for all Android API lavels.

提交回复
热议问题