Java 活动重新创建后,操作栏标题消失

Java 活动重新创建后,操作栏标题消失,java,android,android-actionbar,Java,Android,Android Actionbar,我有三项活动。主活动、设置活动和设置活动 Main打开“设置”,打开“设置” 在设置活动中,我允许用户更改存储在SharedReferences中的语言 在Main和Settings中,我有一个用于SharedReference更改的侦听器 更改时,我重新创建这些活动(主活动和设置)。所有文本均为新语言,但操作栏标题已消失,仅在设备旋转后(以正确的语言)重新出现。用setTitle设置标题不起作用,getTitle返回正确的文本。它只是隐形的 如何在不旋转设备的情况下强制显示ActionBar标

我有三项活动。主活动、设置活动和设置活动

Main打开“设置”,打开“设置”

在设置活动中,我允许用户更改存储在SharedReferences中的语言

在Main和Settings中,我有一个用于SharedReference更改的侦听器

更改时,我重新创建这些活动(主活动和设置)。所有文本均为新语言,但操作栏标题已消失,仅在设备旋转后(以正确的语言)重新出现。用setTitle设置标题不起作用,getTitle返回正确的文本。它只是隐形的

如何在不旋转设备的情况下强制显示ActionBar标题

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ctx = getBaseContext();

    Language.loadLocale(ctx);
    setContentView(R.layout.activity_main);
    act = this;
    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            activeTab = position;
        }
    });


    actionBar.addTab(
            actionBar.newTab()
                    .setText(R.string.title_tab1_active_order_name)
                    .setTabListener(this));

    actionBar.addTab(
            actionBar.newTab()
                    .setText(R.string.title_tab2_history_name)
                    .setTabListener(this));

    actionBar.addTab(
            actionBar.newTab()
                    .setText(R.string.title_tab3_tools_name)
                    .setTabListener(this));

    listenOnLangChange();


}

private void listenOnLangChange(){
    //handles language changes
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            // Implementation
            if (key.equals("setLanguageKey")){
                Configuration config = Language.loadLocale(ctx);
                onConfigurationChanged(config);
            }
        }
    };

    prefs.registerOnSharedPreferenceChangeListener(listener);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    act.recreate();
}
从语言课:

public static Configuration changeLang(String lang, Context ctx)
{

    String languageToLoad  = lang; // your language
    Locale locale = new Locale(languageToLoad);

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;

    ctx.getResources().updateConfiguration(config,
            ctx.getResources().getDisplayMetrics());

    Log.d("CHANGE", languageToLoad);

    return config;
}


public static Configuration loadLocale(Context ctx)
{
    String langPref = "setLanguageKey";
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String language = prefs.getString(langPref, "");
    Log.d("LOAD", language);

    return changeLang(language, ctx);
}

我必须用添加到动作栏的自定义视图来解决这个问题。这是可行的,但也许不是最好的解决方案

public static void setActionbarTitle(Activity act, String title){
    ActionBar actionBar = act.getActionBar();

    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);

    LayoutInflater inflator = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.actionbar_title_layout, null);

    TextView tv = (TextView) v.findViewById(R.id.tv_actionbar_title);
    tv.setText(title);
    tv.setTextColor(Color.WHITE);

    actionBar.setCustomView(v);
}

也许可以将
android:showAsAction=“always”
放在它的xml中?你能发布你的onCreate()吗?感觉在构建视图时遇到了一些问题。我有一个包含三个项目的菜单,上面有showAsAction=“always”。使用新语言正确更新这些内容。不幸的是,只是标题消失了。我从onCreate()和我使用的语言类中添加了一些代码。如果您发现一些奇怪的事情,请告诉我。对于我来说,在
操作栏上调用
setDisplayShowTitleEnabled(true)
是有效的。您的原始代码中没有它,尽管您在回答中将它设置为
false