2014-01-27 46 views
4

使用操作栏选项卡时,有时当选项卡内容对于显示器太大时,它们显示为“堆叠”。当我使用标签内容的自定义视图时,会出现一个问题,它会导致所选标签不会显示在下拉列表中,并且一旦选择了标签,下拉框将消失,并且出现小的空标签。带自定义视图的堆叠的ActionBar选项卡不能正确显示

这里是下拉的屏幕截图,选择项目之前:(请注意,该选项卡的内容不被显示,即使是在选择了选项卡) before selecting item

另外,选择项目之后,标签不再堆积,且选项卡的内容为空: after selecting item

这里是我的代码,(请注意,我使用的是自定义视图的标签只是为了说明问题)

public class ExampleActivity extends Activity { 

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

     final TextView selectedTabText = new TextView(this); 
     setContentView(selectedTabText); 

     ActionBar.TabListener listener = new ActionBar.TabListener() { 
      @Override 
      public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
       TextView customView = (TextView) tab.getCustomView(); 
       selectedTabText.setText(customView.getText()); 
      } 

      @Override 
      public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { 

      } 

      @Override 
      public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 

      } 
     }; 

     ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     addTab(actionBar, listener, "Tab one with a very long name"); 
     addTab(actionBar, listener, "Tab two with a very long name"); 
     addTab(actionBar, listener, "Tab three with a very long name"); 
     addTab(actionBar, listener, "Tab four with a very long name"); 
    } 

    private void addTab(ActionBar actionBar, ActionBar.TabListener listener, String text) { 
     ActionBar.Tab tab = actionBar.newTab(); 
     TextView textView = new TextView(this); 
     textView.setText(text); 
     tab.setCustomView(textView); 
     tab.setTabListener(listener); 
     actionBar.addTab(tab); 
    } 
} 

回答

3

这是一个已知的bug,并已上报bug跟踪:

https://code.google.com/p/android/issues/detail?id=41392

为了解决这个问题,我禁用了讨厌的黑客的动作条倒塌行为。下面的方法可以从在onStart活动方法被调用:

/** 
* SUPER hack to disable tab collapsing with smaller screens, which doesn't allow custom tab bar views to be used 
* https://code.google.com/p/android/issues/detail?id=41392 
*/ 
private void disableCollapsibleTabs() { 
    try { 
     ActionBar actionBar = getActionBar(); 
     Field actionViewField = actionBar.getClass().getDeclaredField("mTabScrollView"); 
     actionViewField.setAccessible(true); 
     Object mTabScrollView = actionViewField.get(actionBar); 

     Method setAllowCollapse = mTabScrollView.getClass().getDeclaredMethod("setAllowCollapse", boolean.class); 
     setAllowCollapse.setAccessible(true); 
     setAllowCollapse.invoke(mTabScrollView, false); 

    } catch (Exception e) { 
     Log.e("", "Error while disabling actionbar collapsible tabs", e); 
    } 
} 
+0

它不android.support.v7.app.ActionBar – RPallas

+0

对不起工作,但它不使用getSupportActionBar(工作时)insted的getActionBar的();我仍然不知道该怎么做。 –

1

编辑:使用

<android.support.design.widget.TabLayout 
    android:id="@+id/tabLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/tint" />` 

,而不是动作条的过时的标签截止了。 这不会折叠方向变化的标签栏,但它似乎谷歌和脸书的应用程序做同样的(我猜)。

更新:似乎它没有工作,这只是一个副作用。 这似乎是一个计时问题 - >当onConfigurationChanged中的代码需要一点时间时,问题似乎消失了。 这确实肮脏的黑客终于工作,但它是不是一个解决方案(不推荐,或许暗示别人来找到它):

@Override 
public void onConfigurationChanged(Configuration newConfig) { 

    try { 
     Thread.sleep(100); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    super.onConfigurationChanged(newConfig); 

} 

对于我这个曾在我的片段时调用。希望能帮助到你。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    final ActionBar actionBar = getActivity()).getSupportActionBar(); 
    actionBar.invalidateOptionsMenu(); 
} 
0

只需在调用onConfigurationChanged方法时重置自定义选项卡。 它适用于我。 希望它有帮助。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 

    try { 
     Thread.sleep(100); 
     actionBar.getTabAt(0).setCustomView(/*your custom view*/); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    super.onConfigurationChanged(newConfig); 


} 
相关问题