How to stop ActionBar Tab navigation to display as a spinner when it gets too long?

后端 未结 5 465
忘掉有多难
忘掉有多难 2020-12-09 11:39

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

相关标签:
5条回答
  • 2020-12-09 11:56

    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.

    0 讨论(0)
  • 2020-12-09 12:00

    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.

    0 讨论(0)
  • 2020-12-09 12:02

    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.

    0 讨论(0)
  • 2020-12-09 12:06

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

    0 讨论(0)
  • 2020-12-09 12:12

    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

    0 讨论(0)
提交回复
热议问题