首页 文章

强制RTL布局方向不适用于app

提问于
浏览
24

我正在尝试在我的应用程序中添加RTL语言支持(特别是现在的阿拉伯语) . 我也会支持英语 . 我做了什么:

  • 将minSdkVersion设置为17

  • AndroidManifest.xml 中将 android:supportsRtl="true" 添加到我的应用程序标签

  • 将我的左/右属性切换为开始/结束

起初我手动进行了这些更改,然后我使用了Android Studio的“重构 - >添加RTL支持尽可能...”菜单项 .

当我预览我的布局文件时,我可以看到RTL预览正确地镜像了UI;但是,即使我使用“强制RTL布局方向”,我的应用程序也不会显示RTL布局 . 系统UI被翻转,因此该选项通常可以正常工作 .

显示RTL布局还需要做些什么吗?我希望我错过了一些明显的东西 . 我在API 21仿真器上测试它 .

更新

我继承了一些代码 . 有些东西可能会覆盖设置并强制进入LTR模式 . 我做了一个测试应用程序来测试RTL模式,它工作正常 . 什么样的代码可能会导致“强制RTL布局方向”设置被忽略(或被覆盖)?

更新2

我已经检查了区域设置是否正确设置,确实如此 . 我还检查了配置,并设置了 ldrtl . 我在签名的apk文件中验证了 android:supportsRtl 使其成为并且没有任何布局文件具有 android:layoutDirection="ltr" . 我甚至尝试手动放置 android:layoutDirection="rtl" 试图强制布局镜像,但这也不起作用

更新3

我在项目中添加了另一个活动,使其成为启动器活动,并确保它没有连接到任何现有代码 . 它是 Activity 的子类 . 这个问题仍然存在 . 所以从理论上讲这是一个配置问题 . 就像我说的那样,我检查了AndroidManifest.xml文件以及生成的所有布局文件,并且RTL支持和布局更改全部进入 . 配置可能出现什么问题?

2 回答

  • 2

    最晦涩的错误 . 正如我在问题中提到的,大部分代码都是继承的 . 它最终成为一个按位运算符问题,正在搞砸应用程序的标志 . 正在使用 &= 而不是 & 来检查是否设置了标志 .

    正如评论中所指出的那样,代码来自_545485中的一个例子,这解释了为什么这么多其他人遇到了同样的问题 . 我提交了一份错误报告,此后博客文章一直在默默更新 . 这是原始代码,我从中删除了 &= . 请勿按原样使用以下代码 . 您需要将 &= 更改为 &

    isDebuggable = (appContext.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    
  • 13

    试试这个...

    • 创建一个类来维护全局应用程序状态 .
    public class YourGlobalClass extends Application {
    
      @Override
      public void onCreate() {
      updateLanguage(this, null);
      super.onCreate();
      }
    
    public static void updateLanguage(Context ctx, String lang) {
    
     Configuration cfg = new Configuration();
     LocalSharedManager manager = new LocalSharedManager(ctx);
     String language = manager.GetValueFromSharedPrefs("force_locale");
    
        if (TextUtils.isEmpty(language) && lang == null) {
           cfg.locale = Locale.getDefault();
           String tmp_locale = "";
           tmp_locale = Locale.getDefault().toString().substring(0, 2);
           manager.SaveValueToSharedPrefs("force_locale", tmp_locale);
    
       } else if (lang != null) {
           cfg.locale = new Locale(lang);
           manager.SaveValueToSharedPrefs("force_locale", lang);
    
       } else if (!TextUtils.isEmpty(language)) {
           cfg.locale = new Locale(language);
       }
       ctx.getResources().updateConfiguration(cfg, null);
      }
    
    }
    
    • 指定 AndroidManifest.xml 标记的全局类,这将导致在创建应用程序/包的进程时使用保存的语言环境对该类进行实例化 . 喜欢, android:name="com.your.package.YourGlobalClass" android:supportsRtl="true"

    • 在MainActivity.java中创建以下两个方法(您可以在任何地方) .

    public class MainActivity extends ActionBarActivity{
    
       .......
    
       // Implement OnclickListener for english_locale button
       findViewById(R.id.english_locale).setOnClickListener(new OnClickListener()
         {
    
            @Override
            public void onClick(View v)
            {
                changeEnglish();
            }
         });
    
        // Implement OnclickListener for arabic_locale button
        findViewById(R.id.arabic_locale).setOnClickListener(new OnClickListener()
           {
    
              @Override
              public void onClick(View v)
               {
                  changeArabic();
                }
           });
    
        /**
        * Method that Update UI for Arabic locale.
        */
        public void changeArabic() {
             new AsyncTask<Void, Void, Void>() {
    
             @Override
             protected Void doInBackground(Void... params) {
                  String app_locale = "ar";
                  Locale locale = new Locale(app_locale);
                  Locale.setDefault(locale);
    
                  //Configuration to query the current layout direction.
                  Configuration config = new Configuration();
                  config.locale = locale;
                  getResources().updateConfiguration(config,
                    getResources().getDisplayMetrics());
                  Bidi bidi = new Bidi(app_locale,
                    Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
                  bidi.isRightToLeft();
                  YourGlobalClass.updateLanguage(getActivity(), "ar");
    
                  //Refreshing current fragment
    
                  Intent i = getActivity().getIntent();
                  startActivity(i);
                  getActivity().finish();
               return null;
              }
    
           }.execute();
         }
    
            /**
            * Method that Update UI for Default(English) locale.
            */
          public void changeEnglish() {
    
              new AsyncTask<Void, Void, Void>() {
    
              @Override
              protected Void doInBackground(Void... params) {
                  String app_locale = "en";
                  Locale locale = new Locale(app_locale);
                  Locale.setDefault(locale);
    
                  //Configuration to query the current layout direction.
                  Configuration config = new Configuration();
                  config.locale = locale;
                  getResources().updateConfiguration(config,
                    getResources().getDisplayMetrics());
                  Bidi bidi = new Bidi(app_locale,
                    Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
                  bidi.isLeftToRight();
                  YourGlobalClass.updateLanguage(getActivity(), "en");
    
                  //Refreshing current fragment
                  Intent i = getActivity().getIntent();
                  startActivity(i);
                  getActivity().finish();
    
              return null;
              }
    
           }.execute();
         }
    
       ......
      //MainActivity end
    }
    
    • 快乐编码......

相关问题