用项目填充操作栏SHerlock

时间:2013-07-26 07:08:20

标签: android actionbarsherlock android-actionbar

我正在实施ActionBarSherlock。我已禁用图标和标题。但我的物品仍然没有占据那个空间。 我用过

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

图标和标题消失了。但是这个空间没有填满我的物品。我有5个项目,如

 <item android:id="@+id/Clear"
    android:title="Clear"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/load"
    android:title="Load"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/Undo"
    android:title="Undo"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/save"
    android:title="Save"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/settings"
    android:title="Settings"
    android:showAsAction="ifRoom"></item>

但是只展示了其中的3个。并且操作栏未满。就像

enter image description here

我希望所有项目都显示在操作栏中。我该怎么办?

编辑:

我正在考虑向栏中添加标签。它给出了运行时错误。这是我添加标签的代码(应该很简单,但我不知道我在这里做错了什么)

Tab tab = getSupportActionBar().newTab();
protected void onCreate(Bundle savedInstanceState) {

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    for (int i = 1; i <= 3; i++) {

        tab.setText("Tab " + i);

        getSupportActionBar().addTab(tab);
    }
    }

我认为此代码应足以向栏添加标签。不是吗?

1 个答案:

答案 0 :(得分:1)

您可以使用onPrepareOptionsMen,如果您使用制表符或viewpager,它可以允许您根据您所在的选项卡更改按钮(只是有一个开关案例):

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    menu.clear();
    switch ((int) _viewPager.getCurrentItem()) {
        case 0:
            getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
            break;
        case 1:
            getSupportMenuInflater().inflate(R.menu.your_menu, menu);
            break;
        case 2:
            getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
            break;
    }
    return super.onPrepareOptionsMenu(menu);
}

或者判断一个简单的菜单

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        getSupportMenuInflater().inflate(R.menu.your_menu, menu);
        return true;
    }

您只需将项目放在菜单xml文件

* 添加标签:*

public class MainActivity extends SherlockFragmentActivity {
private static final String     TAG = MainActivity.class.getSimpleName();
private ViewPager               _viewPager;
private ActionBar               _actionBar;
private Tab                     _Tab;
private TabsAdapter             _tabAdapter;
private int                     _viewPagerOffScreenLimit        = 10;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _viewPager = (ViewPager) findViewById(R.id.viewpager);
        _viewPager.setOffscreenPageLimit(_viewPagerOffScreenLimit);
        _tabAdapter = new TabsAdapter(this, _viewPager);
        _actionBar = getSupportActionBar();
        _actionBar.setTitle(getResources().getString(R.string.app_name));
        _actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        _actionBar.setHomeButtonEnabled(true);

        _tabAdapter.addTab(_actionBar.newTab().setCustomView(getTabIndicator(getString(R.string.tab), R.drawable.ic_launcher)), FragmentTab.class, null);
}
private View getTabIndicator(String text, int drawable) {

        View indicator = _inflater.inflate(R.layout.tabs, null);
        ((TextView) indicator.findViewById(R.id.tab_title)).setText(text);
        ((ImageView) indicator.findViewById(R.id.tab_icon)).setImageResource(drawable);
        return indicator;
    }

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

        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
            ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
        }

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