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

时间:2013-08-09 10:55:14

标签: java android android-actionbar

我有三项活动。主要活动,设置活动和设置活动

主要打开设置,打开设置。

在设置活动中,我让用户更改我在SharedPreferences中存储的语言

在Main和Settings中我有一个关于SharedPreference更改的监听器。

在更改时我重新创建这些活动(主要和设置)。所有文本都使用新语言,但操作栏标题已消失,并且仅在设备旋转后重新出现(使用正确的语言)。使用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);
}

1 个答案:

答案 0 :(得分:1)

我必须通过添加到操作栏的自定义视图来解决它。它可以工作,但也许不是最好的解决方案。

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);
}
相关问题