How to display tabs below action bar

2019-02-19 10:25发布

问题:

I have placed tabs in action bar and it is working fine. but when i rotate device it will appear on the action bar. Is there any way to always display that tab below action bar like

回答1:

Used the following function which force to show stacked tabs

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