当用户在我的应用程序中更改语言时,为什么android:label的文本不更改? [英] Why does android:label's text not changing when user changes language within my app?

查看:301
本文介绍了当用户在我的应用程序中更改语言时,为什么android:label的文本不更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在我的应用程序中,用户看到的第一个活动是选择语言.

So within my app, the very first Activity that user sees is to choose language.

假设用户选择法语,然后转到ActivityB,然后转到ActivityC.

Lets say if user chooses french and then goes to ActivityB, then to ActivityC.

现在决定更改语言.

因此,先回到ActivityB,然后再回到第一个Activity,然后选择语言作为西班牙语.

So goes back to ActivityB and then to very first Activity and choses language as Spanish.

再次,当用户转到ActivityB时,片段/活动中的所有其他文本都更改为西班牙语,但是android:label仍然保留法语.该如何解决?

Now again when user goes to ActivityB, all other text within the fragment/activity is changed to Spanish, but android:label still remains in French. how to fix this?

这是我的ActivityA的外观

This is how my ActivityA looks

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_choose_language, container, false);
        radioGroup = (RadioGroup) rootView.findViewById(R.id.lang_choice_radio);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.english_lang:
                        // do operations specific to this selection
                        setLocale("en_US");
                        Intent intentEng = new Intent(getActivity(), Choose_Country.class);
                        startActivity(intentEng);
                        break;

                    case R.id.indonesian_lang:
                        // do operations specific to this selection
                        setLocale("in");
                        Intent intent = new Intent(getActivity(), Choose_Country.class);
                        startActivity(intent);
                        break;
                }
            }
        });
        return rootView;
    }

    public void setLocale(String lang) {
        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Locale.setDefault(myLocale);
        onConfigurationChanged(conf);
        Intent refreshIntent = new Intent(getActivity(), ChooseLanguage.class); // refresh the activity
        refreshIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        refreshIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(refreshIntent);
        getActivity().finish();
    }

推荐答案

就我而言,应用在更改语言环境后不会更改操作栏语言.当我从最近的应用程序中删除应用程序时,它会改变,从而使该应用程序完全关闭.为了解决此问题,当您要刷新应用程序或使用 oncreate 时,我使用setTitle(R.id.myapplabel),因此无需重新启动应用程序.在string.xml上翻译您的活动标签,它应该可以正常工作.

In my case, app won't change Actionbar language after change locale. It will change when I remove app from the recent app which make the app completely close. To solve, I use setTitle(R.id.myapplabel) when you want to refresh app or oncreate, so no need to restart app. Translate your activity label on string.xml and it should works.

这篇关于当用户在我的应用程序中更改语言时,为什么android:label的文本不更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆