Android 活动标题语言的问题

Android 活动标题语言的问题,android,Android,我的应用程序中有两种语言values/strings.xml和values ru/strings.xml当我以编程方式更改语言时,所有字符串都会转换,但活动标题不变。我在所有活动中都使用 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String languageToLoad = prefs.getString("prefLanguage","en"); Locale locale

我的应用程序中有两种语言values/strings.xmlvalues ru/strings.xml当我以编程方式更改语言时,所有字符串都会转换,但活动标题不变。我在所有活动中都使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String languageToLoad  = prefs.getString("prefLanguage","en");
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
        getBaseContext().getResources().getDisplayMetrics());

当我选择语言时,活动或应用程序将被重新加载。

我使用了另一种方式,它看起来很愚蠢,但它是工作

在每个活动中,尝试从值文件将操作栏标题设置为活动标题

在我的例子中,我使用的是
sherlockactionbar

所以我在我的
productdetails
活动的
oncreate
中添加了这个

    getSupportActionBar().setTitle(getResources().getString(R.string.title_activity_product_details));

最终得到了一个解决方案:


PackageManger
获取活动标题的
resource Id
,并将
AcionBar
的标题设置为资源Id,这一次是因为区域设置已更改,android知道要选择哪个活动标题

try{

    ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), 
        PackageManager.GET_META_DATA);

    ActionBar actionBar = getActionBar();

    if(actionBar != null)
        actionBar.setTitle(activityInfo.labelRes);

}catch (PackageManager.NameNotFoundException ex){

    Log.e(this.getClass().getSimpleName(), 
        "Error while getting activity info. " + ex.getMessage(), ex);

}

我以以下方式完成了这项工作,并为我工作

setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(resources.getString(R.string.app_name));
    setSupportActionBar(toolbar);
更改本地配置后使用此代码。

onCreate()中添加
setTitle(R.string.activity\u title)
会在运行时根据区域设置的更改更新活动标题以及内容本身

以下是一个简单的代码片段,单击该按钮时,该代码片段会将其内容和活动的标题更新为Korea(ko KR):

public class MainActivity extends AppCompatActivity {

    Button btnReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Activity title will be updated after the locale has changed in Runtime
        setTitle(R.string.app_name);

        btnReset = (Button) findViewById(R.id.button);
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Resources resources = getResources();
                DisplayMetrics dm = resources.getDisplayMetrics();
                Configuration conf = resources.getConfiguration();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    conf.setLocale(Locale.KOREA);
                } else {
                    conf.locale = Locale.KOREA;
                }
                resources.updateConfiguration(conf, dm);

                // Overwrite the default Locale
                Locale.setDefault(Locale.KOREA);

                // Clear the back stack then restarts the activity
                startActivity(new Intent(MainActivity.this, MainActivity.class)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK));
            }
        });
    }
}

这也是我的问题。。。请回答这个问题。我从清单中的资源中获得了标题集,但仍然没有进行翻译。如果在AndroidManifest.xml中将标题设置为活动的
标签
,则可以这样重复使用:
int-labelRes=getPackageManager().getActivityInfo(getComponentName(),0).labelRes;如果(labelRes>0){getActionBar.setTitle(labelRes);}
似乎是:我尝试了通过sys菜单进行的区域设置更改和通过在代码中设置区域设置进行的区域设置更改。我使用appcompat和systheme活动在运行棉花糖的设备上进行了尝试。