强制堆叠标签

时间:2012-11-19 18:31:59

标签: android tabs android-actionbar landscape stacked

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

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

谢谢!

4 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分: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 );
    }
}

请注意,我自己并没有想到这一点,只是重写了这个答案中给出的代码:replicate ActionBar Tab(s) with custom view

答案 2 :(得分:2)

我用它来强制从Gingerbread到KitKat的ActionBar堆叠或非堆叠标签。

修改自:http://www.blogc.at/2014/01/23/android-tabs-appear-above-or-below-actionbar/

enter image description here

setHasEmbeddedTabs(mActionbar,false);

    public static void setHasEmbeddedTabs(Object inActionBar, final boolean inHasEmbeddedTabs)
    {
        // get the ActionBar class
        Class<?> actionBarClass = inActionBar.getClass();

        // if it is a Jelly Bean implementation (ActionBarImplJB), get the super class (ActionBarImplICS)
        if ("android.support.v7.app.ActionBarImplJB".equals(actionBarClass.getName()))
        {
            actionBarClass = actionBarClass.getSuperclass();
        }

        // if Android 4.3 >
        if ("android.support.v7.app.ActionBarImplJBMR2".equals(actionBarClass.getName())){
            actionBarClass = actionBarClass.getSuperclass().getSuperclass();
        }

        try
        {
            // try to get the mActionBar field, because the current ActionBar is probably just a wrapper Class
            // if this fails, no worries, this will be an instance of the native ActionBar class or from the ActionBarImplBase class
            final Field actionBarField = actionBarClass.getDeclaredField("mActionBar");
            actionBarField.setAccessible(true);
            inActionBar = actionBarField.get(inActionBar);
            actionBarClass = inActionBar.getClass();
        }
        catch (IllegalAccessException e) {}
        catch (IllegalArgumentException e) {}
        catch (NoSuchFieldException e) {}

        try
        {
            // now call the method setHasEmbeddedTabs, this will put the tabs inside the ActionBar
            // if this fails, you're on you own <img class="wp-smiley" alt=";-)" src="http://www.blogc.at/wp-includes/images/smilies/icon_wink.gif">
            final Method method = actionBarClass.getDeclaredMethod("setHasEmbeddedTabs", new Class[] { Boolean.TYPE });
            method.setAccessible(true);
            method.invoke(inActionBar, new Object[]{ inHasEmbeddedTabs });
        }
        catch (NoSuchMethodException e)        {}
        catch (InvocationTargetException e) {}
        catch (IllegalAccessException e) {}
        catch (IllegalArgumentException e) {}
    }

答案 3 :(得分:1)

如果您需要支持手机和平板电脑,并且不想使用单独的实施,您可以将其添加到您的活动中:

@Override
public Resources getResources() {
    if (mResourcesImpl == null) {
        mResourcesImpl = new ResourcesImpl(super.getResources());
    }
    return mResourcesImpl;
}

class ResourcesImpl extends Resources {
    private Resources mResources;
    private Set<Integer> mActionBarEmbedTabsIds = new HashSet<Integer>();

    ResourcesImpl(Resources resources) {
        super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());

        mResources = resources;

        String packageName = getPackageName();
        mActionBarEmbedTabsIds.add(mResources.getIdentifier("abc_action_bar_embed_tabs", "bool", packageName));
        mActionBarEmbedTabsIds.add(mResources.getIdentifier("abc_action_bar_embed_tabs_pre_jb", "bool", packageName));
        mActionBarEmbedTabsIds.add(mResources.getIdentifier("action_bar_embed_tabs", "bool", "android"));
        mActionBarEmbedTabsIds.add(mResources.getIdentifier("action_bar_embed_tabs_pre_jb", "bool", "android"));
        mActionBarEmbedTabsIds.remove(0);
    }

    @Override
    public boolean getBoolean(int id) throws NotFoundException {
        if (mActionBarEmbedTabsIds.contains(id)) {
            return areActionBarTabsEmbed(); // stacked ot embed goes here
        }
        return super.getBoolean(id);
    }
}