首页 > 解决方案 > 如何在不更改电话语言的情况下更改片段的语言?

问题描述

我注意到我的应用程序仅在我更改电话语言时才更改语言,但仅对于片段,对于活动,它才有效

我在用着

 private void setLocale(String lang) {
        Locale locale= new Locale(lang);
        Locale.setDefault(locale);
        Configuration config=new Configuration();
        config.locale=locale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
        SharedPreferences.Editor editor=getSharedPreferences("Settings",MODE_PRIVATE).edit();
        editor.putString("My_Lang",lang);
        editor.commit();

    }

    public void loadLocale(){
        SharedPreferences editor=getSharedPreferences("Settings",Splash.MODE_PRIVATE);
        String language=editor.getString("My_Lang","My_Lang");
        Log.d("hahahahahahdhjzshsdj",language);
        setLocale(language);


    }

private void showChangeLanguageDialog() {
        final String[] listLang={"Francais","العربية","English"};
        AlertDialog.Builder onBuilder=new AlertDialog.Builder(Splash.this);
        onBuilder.setTitle("Choose Language...");
        onBuilder.setSingleChoiceItems(listLang, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(which==0){
                    setLocale("fr");
                    recreate();
                    Intent intent=new Intent(Splash.this,Splash2.class);
                    startActivity(intent);
                }
                else if(which==1){

                    setLocale("ar");
                    recreate();
                    Intent intent=new Intent(Splash.this,Splash2.class);
                    startActivity(intent);
                }
                else if(which==2){

                    setLocale("en");
                    recreate();
                    Intent intent=new Intent(Splash.this,Splash2.class);
                    startActivity(intent);
                }

                dialog.dismiss();
            }
        });
        AlertDialog mDialog=onBuilder.create();
        mDialog.show();
    }

所以,当我运行应用程序时,我有一个alertDialog选择语言,选择它后,它改变了唯一的不活动但不是在一个片段中,我有 6 个片段

我该如何解决?

标签: androidandroid-fragmentsfragmentlocale

解决方案


推荐阅读