【问题标题】:How to always display ActionBar tabs below ActionBar in Android如何始终在 Android 中的 ActionBar 下方显示 ActionBar 选项卡
【发布时间】:2014-02-18 09:09:38
【问题描述】:

我一直在寻找是否有任何关于如何始终在 ActionBar 下方显示 ActionBar 选项卡的信息,即使手机面向横向也是如此。我知道它会自动将选项卡放置在 ActionBar 中,具体取决于屏幕上是否有足够的空间来容纳它们,但我希望它们始终固定在 ActionBar 下方,即使在横向中也是如此。我发现了一个没有明确答案的类似问题:How to display tabs below action bar。我也知道很多谷歌应用程序,例如; Google Play、Google Music 等都实现了这种设计模式,因此作为一种设计模式显然是可以实现和接受的。

我目前正在使用 ActionBarSherlock 库来创建我的滑动标签导航,如果有人自己想出如何做到这一点,我将不胜感激。提前致谢。

这是我的活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    viewPager = new ViewPager(this);
    viewPager.setId(R.id.pager);
    setContentView(viewPager);

    tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view

    actionBarTabs = getSupportActionBar();
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBarTabs.setCustomView(spinnerView);
    actionBarTabs.setDisplayShowCustomEnabled(true);    

    /* Adds fragments to the tabs adapter */
    tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG1"), Fragment_1.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG2"), Fragment_2.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG3"), Fragment_3.class, null);
}

这是我的 TabsAdapter 代码:

public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener 
{
private final Context context;
private final ActionBar actionBar;
private final ViewPager viewPager;
private final ArrayList<TabInfo> tabsList = new ArrayList<TabInfo>();

/**
 * TabInfo class
 * 
 * Static class containing the tab information.
 * 
 * @since 1.0
 */
static final class TabInfo
{
    private final Class<?> clss;
    private final Bundle args;

    TabInfo(Class<?> _class, Bundle _args)
    {
        clss = _class;
        args = _args;
    }
}

/**
 * Tabs adapter overload constructor.
 * 
 * @param fragmentActivity sets the fragment activity to the adapter.
 * @param refViewPager sets the viewPager variable.
 * @see
 * @since 1.0
 */
public TabsAdapter(SherlockFragmentActivity fragmentActivity, ViewPager refViewPager) 
{
    super(fragmentActivity.getSupportFragmentManager());
    context = fragmentActivity;
    actionBar = fragmentActivity.getSupportActionBar();
    viewPager = refViewPager;
    viewPager.setAdapter(this);
    viewPager.setOnPageChangeListener(this);
}

/**
 * Add tab method to add a tab to the list.
 * 
 * @param tab sets the tab to be added to the tabs in the action bar.
 * @param clss sets the class variable in the TabInfo class.
 * @param args sets the bundle variable in the TabInfo class.
 * @see
 * @since 1.0
 */
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
{
    TabInfo info = new TabInfo(clss, args);
    tab.setTag(info);
    tab.setTabListener(this);
    tabsList.add(info);
    actionBar.addTab(tab);
    notifyDataSetChanged();
}

@Override
public void onPageScrollStateChanged(int state) 
{

}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
{

}

@Override
public void onPageSelected(int position) 
{
    actionBar.setSelectedNavigationItem(position);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) 
{
    viewPager.setCurrentItem(tab.getPosition());
    Object tag = tab.getTag();
    for (int i = 0; i<tabsList.size(); i++)
    {
        if (tabsList.get(i) == tag)
        {
            viewPager.setCurrentItem(i);
        }
    }

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) 
{

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) 
{

}

@Override
public Fragment getItem(int position) 
{
    TabInfo info = tabsList.get(position);
    return Fragment.instantiate(context, info.clss.getName(), info.args);
}

@Override
public int getCount() 
{
    return tabsList.size();
}
}

【问题讨论】:

    标签: android tabs android-actionbar actionbarsherlock


    【解决方案1】:

    我在横向模式下遇到了同样的问题,我在这里找到了解决方案:http://andreimihu.com/blog/2013/10/17/android-always-embed-tabs-in-actionbar/

    基本上,作者希望标签在操作栏内,而你我希望它在外面。因此,只需将方法调用更改为 false(来自上述链接的代码,但稍作修改):

    // This is where the magic happens!
    public void forceTabs() {
        try {
            final ActionBar actionBar = getActionBar();
            final Method setHasEmbeddedTabsMethod = actionBar.getClass()
                .getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
            setHasEmbeddedTabsMethod.setAccessible(true);
            setHasEmbeddedTabsMethod.invoke(actionBar, false);
        }
        catch(final Exception e) {
            // Handle issues as needed: log, warn user, fallback etc
            // This error is safe to ignore, standard tabs will appear.
        }
    }
    

    【讨论】:

    • 我也遇到了同样的问题,并且该功能正常工作,但另一个问题是我只有 2 个选项卡并且它们被推到左侧,有什么办法可以让他们留在中心?
    • 在 ActionBarSherlock 的情况下它不起作用。你有什么想法吗???
    • @arashmoeen 如果在横向模式下,选项卡通常会被推到左侧和中心。我现在没有答案。
    • @AmitJayaswal 该代码用于 Google 的 ActionBar,我没有使用 ActionBarSherlock 进行测试。
    • 非常适合 Holo 主题。使用 Material 主题,选项卡视图有些偏移,导致底部被切断,因此没有指示当前选择的选项卡。需要自己做一些研究......
    【解决方案2】:

    首先,您所描述的“许多 Google 应用程序,例如;Google Play、Google 音乐等都实现了这种类型的设计模式” 不一定是与 ActionBar 相关的导航选项卡。

    有很多实现使用带有标题指示符的viewPager。请参阅PagerSlidingTabStrip 库以获得良好的实现。

    此外,您应该查看官方 android 文档,其中深入讨论了使用选项卡进行导航并显示了获取它的不同方法。检查文档中的这些页面:Providing Descendant and Lateral NavigationCreating Swipe Views with Tabs

    此外,stackoverflow.com 上有很多关于此主题的答案。这个答案中已经提到了我建议的库:https://stackoverflow.com/a/16544501/529138

    检查此问题和相关答案:Tabs below action bar

    这些资源应该足以帮助您实现所需的功能:)

    【讨论】:

    • 非常感谢。我以前遇到过这些答案。我还遇到了Android自己的类PagerTabStrip,我认为可以这样使用。是否可以将其与 ViewPager 结合使用来创建滑动标签导航?
    • 是的,有可能。 PagerTabStrip 是为了与 ViewPager 一起工作而创建的。就个人而言,我从未使用过它。我一直在使用 JakeWharton 的 ViewPagerIndicator 库,但是当我有空闲时间时,我会测试 PagerTabStrip。无论如何,我认为这个链接会对你有所帮助:blog.pboos.ch/post/40575809334/android-pagertabstrip-viewpager
    • 非常感谢您的帮助。我会在某个时候尝试一下,请在您尝试时告诉我结果如何。
    【解决方案3】:
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
    

    actionBar.setLogo(null);

        View homeIcon = findViewById(
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
                        android.R.id.home : android.support.v7.appcompat.R.id.home);
        ((View) homeIcon.getParent()).setVisibility(View.GONE);
        ((View) homeIcon).setVisibility(View.GONE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多