问题
I'm using ActionBarSherlock with NAVIGATION_MODE_TABS. Each tab contains a custom view. While it looks fine in portrait mode, there are numerous problems in landscape mode if the tabs are displayed as drop down list (automatically done by Android):
- Android 4.x: The selected tab is not shown in the Action Bar (see image, red circle). The same happens when using the built-in Action Bar from the Android SDK directly.
- Android 2.x: The selected tab is not shown in the Action Bar. As soon as I click on the drop down, the whole drop down vanishes and I get empty tabs instead.
Everything works fine if I do not use a custom view. My code that creates the ActionBar on the image (kept simple on purpose):
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 0; i < 7; i++) {
ActionBar.Tab tab = getActionBar().newTab();
TextView view = new TextView(this);
view.setText("This is tab " + (i + 1));
tab.setCustomView(view);
tab.setTabListener(this);
getActionBar().addTab(tab);
}
Am I doing something wrong?
EDIT: To clarify, getSupportActionBar() is present in the original code and the Action Bar from ActionBarSherlock is working fine apart from the described issues. I used the code sample above to illustrate that the first issue (selected tab not shown in Action Bar) is also present when using the built-in Action Bar. Sorry for the confusion.
回答1:
With ActionBarSherlock you should be using getSupportActionBar() in order for it to work on 2.x
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
The text color is either coming from the custom view you are applying to the Tab or from a theme you are applying at the Activity level.
回答2:
this method works fine for me:
JUST PUT THE NAVIGATION METHOD, AFTER ADDING ADDING TABS:
... // adding tabs
bar.setNavigationMode(ActionBar.Navigation_mode_tabs);
goodLuck ;)
来源:https://stackoverflow.com/questions/13430542/actionbarsherlock-tab-loses-custom-view-in-landscape-mode