openOptionsMenu function not working in ICS?

后端 未结 8 2016
一整个雨季
一整个雨季 2020-12-03 21:07

Im using action bar compability library. Im trying to open the options menu from a button with openOptionsMenu() function but it does nothing.

Menu shows as usual wh

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 22:05

    Do you mean you want to show a button on the right side of the action bar?

    Here is how I did mine:

    res/menu/main.xml

    
    
    
    

    Activity
    1) take note of the ActionBarActivity; 2) MenuInflater in onCreateOptionsMenu 3) onOptionsItemsSelected (I think you need to return super.onOptionsItemSelected(item) )

    public class BaseActivity extends ActionBarActivity {
        ....
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
    
        return true;
    }
    ....
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
        case R.id.menu_share:
    
    
           //Do something
            break;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

提交回复
热议问题