强制堆叠标签

Lop*_*ela 5 tabs android landscape stacked android-actionbar

有没有办法强制堆叠标签?我希望标签分离动作栏(在第二行),即使在横向模式下也是如此.

我试图强迫它,但我不能.例如,Android中的Twitter应用程序,当更改为lanscape模式时,继续显示两行(单独行中的选项卡,称为堆叠选项卡).

谢谢!

Com*_*are 8

你不仅不能强制堆叠标签,你甚至不能强制标签 - Android可以并将用一个下拉列表替换它们,以便在某些屏幕尺寸和方向上进行导航.

您唯一的解决方案是远离操作栏标签,例如使用ViewPagerPagerTabStrip.


Jes*_*erB 5

我很幸运使用以下反射'hack':

private void forceStackedTabs() {
    ActionBar ab = getSupportActionBar();
    if ( ab instanceof ActionBarImpl ) {
        // Pre-ICS
        disableEmbeddedTabs( ab );
    } else if ( ab instanceof ActionBarWrapper ) {
        // ICS
        try {
            Field abField = ab.getClass().getDeclaredField( "mActionBar" );
            abField.setAccessible( true );
            disableEmbeddedTabs( abField.get( ab ) );
        } catch (NoSuchFieldException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        } catch (IllegalArgumentException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        } catch (IllegalAccessException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        }
    }
}
private void disableEmbeddedTabs(Object ab) {
    try {
        Method setHasEmbeddedTabsMethod = ab.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(ab, false);
    } catch (Exception e) {
        Log.e( TAG, "Error disabling actionbar embedded", e );
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我自己并没有想到这一点,只是重写了这个答案中给出的代码:复制带有自定义视图的ActionBar选项卡