android - 使用 showAsAction ="ifRoom"未在 ActionBar 中显示操作项

标签 android android-actionbar

我正在尝试向我的支持操作栏添加一些操作项。在我的 Activity 中,我还向操作栏添加了选项卡。

这是 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<ShowEmailsFragment>(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);
    }
}

LoadTabsInBackground 类在执行一些数据库操作后将一些选项卡添加到 ActionBar。

这是我正在膨胀的菜单资源:

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

    <item
        android:id="@+id/email_refresh"
        android:orderInCategory="1"
        android:showAsAction="ifRoom"
        android:title="@string/action_refresh"
        android:icon="@drawable/ic_menu_refresh"/>

    <item
        android:id="@+id/email_bulk_seen"
        android:orderInCategory="20"
        android:showAsAction="ifRoom"
        android:title="@string/action_seen"
        android:icon="@android:drawable/sym_action_email"/>

    <item
        android:id="@+id/email_bulk_delete"
        android:orderInCategory="40"
        android:showAsAction="ifRoom"
        android:title="@string/action_delete"
        android:icon="@android:drawable/ic_menu_delete"/>

</menu>

这里是 AndroidManifest.xml 的摘录,您可以在其中看到我使用的主题是 Theme.AppCompat.Light:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.dndonline.battleclient4android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >

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


        <activity
            android:name="it.dndonline.battleclient4android.Activities.ShowEmails"
            android:label="@string/title_activity_show_folders"
            android:theme="@style/Theme.AppCompat.Light" >
        </activity>

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

    </application>

</manifest>

对我来说一切似乎都是正确的,不幸的是,尽管选项卡加载正确,这意味着 ActionBar 正常工作,操作栏中没有加载任何菜单项。将 showAsAction 值设置为 always 不会改变任何内容。

我正在 Android 2.3.3 上测试它。

最佳答案

我发现了问题。菜单 .xml 文件中存在错误。事实上,我添加了一个新的命名空间:

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

但是,我仍然引用该属性,就好像它属于 android 命名空间一样:

android:showAsAction="ifRoom"

引用此属性的正确方法是使用:

app:showAsAction="ifRoom"

因为它属于命名空间app

这是文档中的相关部分:

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.)

关于android - 使用 showAsAction ="ifRoom"未在 ActionBar 中显示操作项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22044847/

相关文章:

android - 如何获取 Android 设备的当前方向 (ActivityInfo.SCREEN_ORIENTATION_*)?

android - 当特定子项具有焦点时禁用 ViewPager 滚动

java - android 自定义actionbar onclicklistener

android - AppCompat ShareActionProvider 图标与其他图标相比太大

java - Android OnNavigationListener 覆盖?

android - TabLayout 中的 fragment 仅在用户滑动 Android 时加载

android - 如何检测用户点击了插页式广告?

android - 如何在 xml 中更改回收器 View 的方向?

android - 我想为所有 Activity 保留一个操作栏

android - 未应用操作栏样式