ActionBar不显示菜单项

时间:2015-01-11 19:19:44

标签: android

我将main.xml定义为:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/notifications"
        android:icon="@drawable/ic_notifications"
        android:title="@string/notifications" />
    <item
        android:id="@+id/reportsView"
        android:icon="@drawable/ic_reports"
        android:showAsAction="always"
        android:title="@string/reportsView" />
    <item
        android:id="@+id/userProfile"
        android:icon="@drawable/ic_profile"
        android:title="@string/userProfile" />
</menu>

在活动中我有这个:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

但我仍然无法看到操作栏上的按钮。任何人都可以告诉我缺少什么

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@null</item>
        <item name="metaButtonBarStyle">@style/ButtonBar</item>
        <item name="metaButtonBarButtonStyle">@style/ButtonBarButton</item>
    </style>

    <style name="ButtonBar">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingRight">2dp</item>
        <item name="android:paddingBottom">0dp</item>
        <item name="android:background">@android:drawable/bottom_bar</item>
    </style>

    <style name="ButtonBarButton" />

</resources>

2 个答案:

答案 0 :(得分:0)

这些项目不会出现在您的ActionBar中,因为您没有明确告诉它们可见:

android:showAsAction="always"

<击> 如果您正在使用支持包中的活动类(例如ActionBarActivity),则还需要使用这样的自定义命名空间:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/notifications"
        android:icon="@drawable/ic_notifications"
        myapp:showAsAction="always"
        android:title="@string/notifications" />
    <item
        android:id="@+id/reportsView"
        android:icon="@drawable/ic_reports"
        myapp:showAsAction="always"
        android:title="@string/reportsView" />
    <item
        android:id="@+id/userProfile"
        android:icon="@drawable/ic_profile"
        myapp:showAsAction="always"
        android:title="@string/userProfile" />
</menu>

<击>

答案 1 :(得分:0)

这不是很有说服力,但也会覆盖它......

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    return true  //return super.onPrepareOptionsMenu(menu);
}

或者清理你的项目..重新启动......所有这些