Java 操作栏自定义组件(自定义开关)未与actionLayout一起显示

Java 操作栏自定义组件(自定义开关)未与actionLayout一起显示,java,android,xml,android-actionbar,Java,Android,Xml,Android Actionbar,我想在操作栏中创建一个自定义开关。这是我的菜单xml: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:autolux="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/actionbar_notification_switch" android:title="" android

我想在操作栏中创建一个自定义开关。这是我的菜单xml:

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

<item
    android:id="@+id/actionbar_notification_switch"
    android:title=""
    android:actionLayout="@layout/notification_switch"
   autolux:showAsAction="always"/>
</menu>
但是我的actionbar没有显示我的自定义开关(或者任何其他组件,如果我把它放在通知_switch.xml中的话)。此外,我还可以将main.xml菜单设置为我的菜单,它只使用一个图标,可以正常工作。该代码是:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:autolux="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_bar_filter"
        android:icon="@drawable/ic_sort"
        android:title="@string/action_settings"
        autolux:showAsAction="always" />

</menu>

我似乎无法找出我的自定义组件出了什么问题。任何帮助都将不胜感激。(其他信息是:Min Sdk版本14,Compile Sdk版本19,Build Tools版本20

我希望它能帮助:

删除:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.notification_menu, menu);
    return true;
}
然后在您的
onCreate方法中

 ActionBar mActionBar = getActionBar();
 LayoutInflater mInflater = LayoutInflater.from(this);
 View mCustomView = mInflater.inflate(R.layout.notification_switch, null);
 mActionBar.setCustomView(mCustomView);
 mActionBar.setDisplayShowCustomEnabled(true);
参考:

如果您使用的是
actionBarCompat
请使用
getSupportActionBar
而不是
getActionBar

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.notification_menu, menu);
    return true;
}
 ActionBar mActionBar = getActionBar();
 LayoutInflater mInflater = LayoutInflater.from(this);
 View mCustomView = mInflater.inflate(R.layout.notification_switch, null);
 mActionBar.setCustomView(mCustomView);
 mActionBar.setDisplayShowCustomEnabled(true);