How to stop ActionBar Tab navigation to display as

2019-01-17 22:32发布

问题:

i have an action bar with a Tab navigation. While running on 3.1 everything was fine, but once i installed the app on a 4.1 device the tabs where forced to be displayed as a spinner, wrecking my layout design. Looks like the tabs are taking too much space (if i remove some of the fragments everyting looks ok again. Is there a way to stop this behaviour and display the navigation as tabs again just as in android 3.1? Does anyone know?

thanks!

回答1:

I found the answer here. Had to set the navigation mode AFTER adding the tabs.



回答2:

First of, just to clarify: This is not a bug and it works as intended. Reference this discussion on the Google code forums.

However, I came to the solution that if you override:

<bool name="abs__action_bar_embed_tabs">false</bool> //for ActionBarSherlock
<bool name="android:action_bar_embed_tabs">false</bool> //for default ActionBar

You won't have a NAVIGATION_MODE_LIST in portrait mode. However you won't have embedded tabs and if you rotate your screen to landscape it won't work either. By default you'll have embedded tabs and therefore a NAVIGATION_MODE_LIST on a screen with a width of >480dp.

This behavior occurs (I assume) because the embedded tabs are limited to the width of the ActionBar, so if you override the boolean value it'll have tabs in a separate row and it won't collapse. But unfortunately I can't explain myself why this does not work in landscape.



回答3:

Sorry, but you can't stop this. This is a feature, not a bug, according to Google. See: http://code.google.com/p/android/issues/detail?id=24439



回答4:

As I understand you should consider using a ViewPager + PagerTitleStrip if you have many tabs and want to make them scrollable all time. Here is a quote from ttps://code.google.com/p/android/issues/detail?id=24439#c9:

If your UI contains many tabs to the point where you hit the scrolling tabs or collapse-to-spinner case regularly, you might consider using a PagerTitleStrip as an indicator rather than full tabs to present this info in a less cluttered way. This can be especially useful if the set of tabs displayed is under user control. Clickable tabs work best when there is a small, bounded set such as in the YouTube app. Scrolling tab strips lose one-touch access to any tab, their primary advantage over a simple title strip. Examples of the PagerTitleStrip style can be found in the Android Market and Google+ apps.

I would not recommend using tricks as nobody guarantee that tricks will works stable.



回答5:

If you really want to still use tabs and stop it from collapsing to a spinner, it is technically possible using reflection IF you happen to be setting a customView for the tabs:

View v = <your custom tab view>;
Tab tab = actionBar.newTab().setCustomView(v);
    do{
       v = (View)v.getParent();                 
    } while (v!=null && !v.getClass().getSimpleName().equalsIgnoreCase("ScrollingTabContainerView"));

if(v!=null) {
    try {
        Method allowCollapse = v.getClass().getDeclaredMethod("setAllowCollapse", new Class[] { Boolean.TYPE });
        allowCollapse.setAccessible(true);
        allowCollapse.invoke(v, new Object[]{ false });                         
    } catch (Exception e) {
    }
}

If there is not enough space, the tabs will scroll in the ActionBar. The Tabs and any Menu actionItems are given 1/2 the screen width so you can calculate if the tabs will end up scrolling or not and make adjustments to tabs, menu actionItem labels etc or force the tabs into a stacked mode to avoid the scrolling tabs... OR as previously suggested, using the PagerTitleStrip instead.