如何强制拆分ActionBar的选项卡、标题/主页/菜单?

5
我正在使用PageViewerActionBar的选项卡功能,但是遇到了一个问题。在我的Nexus 7上,选项卡显示如下图所示:

On Nexus 7

由于空间不足,您可以看到标题被切割了。但是,在安装了Android 4.0的HTC Incredible S上,它显示如下:

enter image description here

这正是我想要的。实际上,我想要选项卡栏位于屏幕底部,尽管将其分成两行并将其放在顶部也可以接受。
有人知道如何强制使Nexus 7上的应用程序将选项卡栏分成另一行吗?谢谢!
4个回答

15
据我所知,仅有操作栏决定是否将选项卡放在第二行,我们无法影响其决定。此外,请记住,在许多情况下,您的选项卡将不会出现,而是被下拉列表所替代 - 基本上,Android将选项卡导航转换为列表导航。
如果您想确保您的选项卡始终是选项卡,并始终位于操作栏下方,请从操作栏中删除选项卡并转而使用ViewPager作为内容,其中包括来自Android支持包的PagerTabStripViewPager的来源)或ViewPagerIndicator项目的选项卡指示器。这样做的另一个好处是,您的内容现在可以水平滑动以在选项卡之间移动,这是最流行的方法之一。

实际上,我想将选项卡栏放置在屏幕底部

请注意,这违反了Android设计指南:http://developer.android.com/design/patterns/pure-android.html

7
我使用这个方法来强制将ActionBar的选项卡从Gingerbread到KitKat中进行堆叠或不堆叠。
修改自:http://www.blogc.at/2014/01/23/android-tabs-appear-above-or-below-actionbar/ http://i.imgur.com/fO0Vk3V.png
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) {}
    }

这个很好用!我想指出它需要在方向改变时进行回调。 - Chris Allen

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);
    }
}

-1

无论如何,要避免选项卡变成微调器,请在添加选项卡后设置导航模式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接