简体   繁体   中英

android - Action bar sherlock add unnecessary button

i use action bar sherlock and i set custom layout in the menu:

<item
    android:id="@+id/action_menu"
    android:actionLayout="@layout/menu_item"
    android:icon="@drawable/b_menu"
    android:orderInCategory="1"
    android:showAsAction="always"/>
<item
    android:id="@+id/action_audio"
    android:actionLayout="@layout/audio_item"
    android:icon="@drawable/b_audio"
    android:orderInCategory="2"
    android:showAsAction="always"/>
<item
    android:id="@+id/action_mySong_details"
    android:actionLayout="@layout/my_song_properties"
    android:icon="@drawable/b_mysong"
    android:orderInCategory="2"
    android:showAsAction="always"/>

to control those items i use:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /** Create an option menu from res/menu/items.xml */
    getSherlock().getMenuInflater().inflate(R.menu.main_tab, menu);

    /** Get the action view of the menu item whose id is search */
    View v = (View) menu.findItem(R.id.action_menu).getActionView();
    Button b = (Button) v.findViewById(R.id.btnMenu);
    return super.onCreateOptionsMenu(menu);
}

for some reason the action bar add unnecessary item, does anybody knows how this case happene?

You are using Action bar sherlock. So use SupportMenuInflater.

  public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
  com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
  inflater.inflate(R.layout.menu, menu);
  }

  public boolean onOptionsItemSelected(MenuItem item) {
  // Handle item selection
  switch (item.getItemId()) {
  case R.id.action_menu:
     // Intent i=new Intent(A.this,B.class);
      //startActivity(i);
      return true;
  }
  return false;

}

我发现了错误:我从所有项目中删除了android:orderInCategory,现在可以了,在这种情况下,我不需要此选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM