Action Items not showing in ActionBar with showAsAction=“ifRoom”

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I'm trying to add some Action Items to my Support Action Bar. In my activity, I have also tabs added to the Action Bar.

This is an excerpt of the activity:

public class ShowEmails extends ActionBarActivity implements ShowEmailsFragmentInteractionListener  {      private IMAPClientService service;     private boolean bound;      private ActionBar ab;      private MailDBHelper mdbhelper;     private SQLiteDatabase db;      private Intent client_service;      <.........................>      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          this.client_service = new Intent(this, IMAPClientService.class);          this.mdbhelper = new MailDBHelper(this.getApplicationContext(), MailDBHelper.MAIL_DB_NAME, null, MailDBHelper.MAIL_DB_VERSION);         this.db = this.mdbhelper.openWriteable();          this.ab = this.getSupportActionBar();         this.ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);         this.ab.show();          Tab t = ab.newTab().setText(R.string.all_emails)                 .setTabListener(new TabListener(this, "all", ShowEmailsFragment.class));          ab.addTab(t);          new LoadTabsInBackground().execute();     }        @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.show_emails_bulk_action, menu);          return super.onCreateOptionsMenu(menu);     } }

The class LoadTabsInBackground adds some tabs to the ActionBar after doing some database operation.

This is the menu resource I'm inflating:

                    

And here is an excerpt from AndroidManifest.xml, where you can sse the theme I'm using is Theme.AppCompat.Light:

                                       <.......................>                              <......................................>        

Everything seems correct to me, unfortunately, although tabs are loaded correctly, this means that ActionBar is properly working, none of the menu items are loaded in the action bar. Setting the showAsAction value to always doesn't change anything.

I'm testing it on Android 2.3.3.

回答1:

I found the problem. There was an error in the menu .xml file. In fact, I've added a new namespace:

xmlns:app="http://schemas.android.com/apk/res-auto"

But then, I still refer the property as if it belongs to the android namespace:

android:showAsAction="ifRoom"

The right way to refer this property is to use:

app:showAsAction="ifRoom"

Cause it belongs to the namespace app.

Here is the relevant part in documentation:

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)



回答2:

I found the same error, and i resolve it like this

add,xmlns:app="http://schemas.android.com/apk/res-auto" in the middle of the code, works for me :)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!