2017-09-19 81 views
5

Kể từ khi tôi nâng cấp lên android oreo trên thiết bị di động, hỗ trợ RTL cho ứng dụng của tôi không hoạt động. nó thay đổi các chuỗi sang tiếng Ả Rập nhưng không thay đổi hướng bố cục. nhưng nếu tôi chạy cùng một RTL chuyển sang bất kỳ thiết bị nào thấp hơn oreo, mọi thứ đều hoạt động tốt. bất cứ ai khác có kinh nghiệm vấn đề này? có bất kỳ tuyên bố chính thức nào về lỗi này và cách giải quyết khác không?Lỗi bố cục RTL trong android Oreo

Dưới đây là phương pháp của tôi để thay đổi vị trí

public static boolean setDefaultLocale(Context context) { 
    Resources resources = context.getResources(); 
    PreferenceManager preferenceManager = PreferenceManager.getInstance(); 
    String localLanguage = resources.getConfiguration().locale.getLanguage(); 
    boolean isLanguageChanged = !preferenceManager.getCurrentLanguageCode().equalsIgnoreCase(localLanguage); 
    if (isLanguageChanged) { 
     Log.d("", preferenceManager.getCurrentLanguageCode()); 
     Locale locale = new Locale(preferenceManager.getCurrentLanguageCode()); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
      Locale.setDefault(Locale.Category.DISPLAY, locale); 
     else 
      Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     resources.updateConfiguration(config, resources.getDisplayMetrics()); 
     ((Activity) context).recreate(); 
    } 
    return isLanguageChanged; 
} 
+0

Bạn có bật RTL trong tệp kê khai không? –

+0

@SamuelRobert: yes android: supportRtl = "true" –

+0

@SamuelRobert: chỉ để làm rõ thực tế, mọi thứ đều hoạt động tốt như mong đợi ngay cả bố cục RTL chuyển đổi dưới oreo nhưng không có trong thiết bị oreo. –

Trả lời

3

Nhờ @amorenew và tinh chỉnh phương pháp này trong util lớp để hỗ trợ cho bản cập nhật lạ này trong oreo bên dưới là phương pháp làm việc bạn chỉ cần gọi phương thức này onResume bất cứ khi nào người dùng thay đổi tùy chọn ngôn ngữ ứng dụng

/** 
* this to change app language to the saved language in user preferences 
* 
* @param context 
* @return 
*/ 
public static boolean setDefaultLocale(Context context, boolean isClearData) { 
    Resources resources = context.getResources(); 
    Resources resourcesApp = context.getApplicationContext().getResources(); 
    String localLanguage = resources.getConfiguration().locale.getLanguage(); 
    boolean isLanguageChanged = !PreferenceManager.getInstance().getCurrentLanguageCode().equalsIgnoreCase(localLanguage); 
    if (isLanguageChanged) { 
     Log.d("", PreferenceManager.getInstance().getCurrentLanguageCode()); 
     Locale locale = new Locale(PreferenceManager.getInstance().getCurrentLanguageCode()); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     resources.updateConfiguration(config, resources.getDisplayMetrics()); 
     resourcesApp.updateConfiguration(config, resources.getDisplayMetrics()); 
     //for API 25 
     Configuration configuration = resources.getConfiguration(); 
     configuration.setLocale(locale); 
     context.getApplicationContext().createConfigurationContext(configuration); 
     context.createConfigurationContext(configuration); 

     ((Activity) context).recreate(); 
     if (isClearData) { 
      CurrencyViewModel.getInstance().removeModel(); 
      CarNationalityViewModel.getInstance().removeModel(); 
      DialCodeViewModel.getInstance().removeModel(); 
     } 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
      ((Activity)context).getWindow().getDecorView().setLayoutDirection(Locale.getDefault().getLanguage().equalsIgnoreCase("ar") 
        ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR); 
     } 
    } 
    return isLanguageChanged; 
} 
+0

Bạn nên chấp nhận câu trả lời của @amorenew –

4

Simple sửa chữa chức năng onCreate của bạn thêm đoạn mã sau:

if (Locale.getDefault().getLanguage()=="ar") 
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 
else 
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR); 
Các vấn đề liên quan