如何在使用ActionBarSherlock时设计自定义选项卡

时间:2012-06-15 17:14:53

标签: android actionbarsherlock

我一直在尝试设置ActionBarSherlock的样式,以便为我的应用程序提供我想要的外观,但是虽然我已经成功了,但我一直在使用Tabs(可以使用Fragments刷卡)。到目前为止,我没有给它自定义背景和文本颜色,同时还删除了标签之间的边框。

我目前拥有的是

enter image description here

而我想让它看起来像是什么 enter image description here

这是我在活动中使用的代码,其中大部分是我从SO上的另一个问题中借用的(我会在找到它后立即将其链接):

public class HomeActivity extends SherlockFragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    mTabsAdapter = new TabsAdapter(this, mViewPager);


    mTabsAdapter.addTab(bar.newTab().setText("Tab 1"),
            MyFragment.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Tab 2"),
            MyFragment.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Tab 3"),
            MyFragment.class, null);
}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

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

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

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

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

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

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

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
    }
}

1 个答案:

答案 0 :(得分:0)

避免FragmentPagerAdapter的继承来做你想要的。您应该将viewPager与viewPagerIndicator库一起使用:ViewPagerIndicator 看看,SJ(Super Jake)非常容易使用和开发