【问题标题】:How to display tabs below action bar如何在操作栏下方显示选项卡
【发布时间】:2013-11-18 20:23:42
【问题描述】:

我在操作栏中放置了选项卡,它工作正常。但是当我旋转设备时,它会出现在操作栏上。有什么方法可以始终在操作栏下方显示该选项卡,例如

【问题讨论】:

    标签: android tabs android-actionbar


    【解决方案1】:

    使用以下函数强制显示堆叠的标签

    private void forceStackedTabs(ActionBar ab)
        {
            try
            {
                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 (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
    private void disableEmbeddedTabs(Object ab)
        {
            try
            {
                Method setHasEmbeddedTabsMethod = ab.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
                setHasEmbeddedTabsMethod.setAccessible(true);
                setHasEmbeddedTabsMethod.invoke(ab, false);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    

    【讨论】:

    • 我评论了 if-else if 因为我不知道 ActionBarImpl 和 ActionBarWrapper 是什么意思,它可以工作。谢谢! (如果你想向我解释,那就太棒了:D)
    • 嘿,我对此发表了评论。在 ICS 4.0 之前,ActionBar 是 ActionBarImpl 类的实例,而在 ICS 中,它是 ActionBarWrapper 类的实例。在不同的操作系统上测试此功能。
    • 谢谢!你的代码很适合我。附加说明:我认为 ActionBarWrapper 仅在您使用 ActionBarSherlock 库时适用。就我而言,我没有,android默认实现是它的内部 ActionBarImpl 类。
    • @Sandy - 我尝试使用它。但是我得到了 java.lang.NoSuchFieldException 之类的异常: mActionBar 在我的代码中,ActionBar 变量被声明为 _actionBar。所以,我改变了 Field abField = ab.getClass().getDeclaredField("mActionBar"); as Field abField = ab.getClass().getDeclaredField("_actionBar");但是,我仍然遇到类似的异常。对此有什么想法吗?
    • 你能帮我解决 ActionBar 的问题吗,例如 supportlibrary v7.我没有收到这样的方法错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多