在语言环境更改中,API 24及更高版本中的Android RTL问题

在语言环境更改中,API 24及更高版本中的Android RTL问题,第1张

概述我试图在运行时更改应用程序的区域设置.它在API级别24以下的Andorid中工作正常.但是在API级别24或更高级别,布局方向不会根据区域设置而改变.下面是在运行时更改语言环境的代码.我使用了LocaleHelper类,如下所示publicclassLocaleHelper{privatestaticfinalStringS

我试图在运行时更改应用程序的区域设置.它在API级别24以下的AndorID中工作正常.但是在API级别24或更高级别,布局方向不会根据区域设置而改变.
下面是在运行时更改语言环境的代码.我使用了LocaleHelper类,如下所示

public class LocaleHelper {    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";    public static Context onAttach(Context context) {        String lang = getPersistedData(context, Locale.getDefault().getLanguage());        return setLocale(context, lang);    }    public static Context onAttach(Context context, String defaultLanguage) {        String lang = getPersistedData(context, defaultLanguage);        return setLocale(context, lang);    }    public static String getLanguage(Context context) {        return getPersistedData(context, Locale.getDefault().getLanguage());    }    public static Context setLocale(Context context, String language) {        persist(context, language);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            return updateResources(context, language);        }        return updateResourcesLegacy(context, language);    }    private static String getPersistedData(Context context, String defaultLanguage) {        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);    }    private static voID persist(Context context, String language) {        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);        SharedPreferences.Editor editor = preferences.edit();        editor.putString(SELECTED_LANGUAGE, language);        editor.apply();    }    @TargetAPI(Build.VERSION_CODES.N)    private static Context updateResources(Context context, String language) {        Locale locale = new Locale(language);        Log.d("LocaleHelper", "language : "+language);        Locale.setDefault(locale);        Configuration configuration = context.getResources().getConfiguration();        configuration.setLocale(locale);        configuration.setLayoutDirection(locale);        return context.createConfigurationContext(configuration);    }    @SuppressWarnings("deprecation")    private static Context updateResourcesLegacy(Context context, String language) {        Locale locale = new Locale(language);        Locale.setDefault(locale);        Resources resources = context.getResources();        Configuration configuration = resources.getConfiguration();        configuration.locale = locale;        configuration.setLayoutDirection(locale);        resources.updateConfiguration(configuration, resources.getdisplayMetrics());        return context;    }}

以下是我在我的活动类中使用的代码

public class MainActivity extends AppCompatActivity {    @BindVIEw(R.ID.TitleTextVIEw)    TextVIEw mTitleTextVIEw;    @BindVIEw(R.ID.descTextVIEw)    TextVIEw mDescTextVIEw;    @BindVIEw(R.ID.aboutTextVIEw)    TextVIEw mAboutTextVIEw;    @BindVIEw(R.ID.toTRbutton)    button mToTRbutton;    @BindVIEw(R.ID.toENbutton)    button mToENbutton;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        ButterKnife.bind(this);        setTitle(getString(R.string.main_activity_toolbar_Title));    }    @OverrIDe    protected voID attachBaseContext(Context base) {        super.attachBaseContext(LocaleHelper.onAttach(base));    }    @OnClick(R.ID.toTRbutton)    public voID onChangetoTRClicked() {        updateVIEws("ur");    }    @OnClick(R.ID.toENbutton)    public voID onChangetoENClicked() {        updateVIEws("en");    }    private voID updateVIEws(String languageCode) {        Context context = LocaleHelper.setLocale(this, languageCode);        Resources resources = context.getResources();        mTitleTextVIEw.setText(resources.getString(R.string.main_activity_Title));        mDescTextVIEw.setText(resources.getString(R.string.main_activity_desc));        mAboutTextVIEw.setText(resources.getString(R.string.main_activity_about));        mToTRbutton.setText(resources.getString(R.string.main_activity_to_tr_button));        mToENbutton.setText(resources.getString(R.string.main_activity_to_en_button));        setTitle(resources.getString(R.string.main_activity_toolbar_Title));        this.recreate();    }}

在我的应用程序类中,我添加了以下代码

@OverrIDe    protected voID attachBaseContext(Context base) {        super.attachBaseContext(LocaleHelper.onAttach(base, "en"));    }

现在,当我将语言环境从英语更改为乌尔都语时,语言会发生变化,但布局方向不会按预期更改.当我再次点击乌尔都语然后布局方向改变(第二次尝试).以下是参考的截图

请帮助解决问题

解决方法:

问题似乎是它在第一次更新时没有反映布局方向的变化.我通过覆盖Activity的onAttachedToWindow方法解决了这个问题,如下所示:

@OverrIDepublic voID onAttachedToWindow() {    super.onAttachedToWindow();    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {        getwindow().getDecorVIEw().setLayoutDirection(                "ur".equals(LocaleHelper.getLanguage(this)) ?                VIEw.LAYOUT_DIRECTION_RTL : VIEw.LAYOUT_DIRECTION_LTR);    }}

在API 25上测试,它工作正常.虽然我目前还不确定这种方法有什么副作用,但要小心.不过,我认为这正是你要找的.

我还将更新blog post以反映更优雅,通用的代码.

总结

以上是内存溢出为你收集整理的在语言环境更改中,API 24及更高版本中的Android RTL问题全部内容,希望文章能够帮你解决在语言环境更改中,API 24及更高版本中的Android RTL问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097117.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存