2016-01-28 46 views
1

所以我最近尝试添加的菜单图标之外的另一个动作按钮在工具栏上:动作按钮没有显示出来

enter image description here

但通过添加动作按钮下面的教程,我无法将它显示在我的工具栏上。

这里是我的菜单/ menu_notify.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="com.example.jovie.canteen.MenuNotify"> 
    <item 
     android:id="@+id/action_notify" 
     android:icon="@drawable/ic_notifications_black_24px" 
     android:title="@string/action_notify" 
     app:showAsAction="always" /> 
</menu> 

我隔上一个新的类,这是MenuNotify从我的主类我认为新的行动按钮,不知道这是增加新的动作按钮的正确方法在工具栏上。

MenuNotify.java:

package com.example.jovie.canteen; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

/** 
* Created by Jovie on 1/28/2016. 
*/ 
public class MenuNotify extends AppCompatActivity { 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_notify, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
// Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     switch (item.getItemId()) { 
      //noinspection SimplifiableIfStatement 
      case R.id.action_notify: 
       startActivity(new Intent(this, Home.class)); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

顺便说一句,我的过滤器图标按钮,在我的主类。编辑: 感谢g2o的帮助!

enter image description here

+0

只想验证您正在使用的活动类是否正在扩展'MenuNotify'? –

+0

是的,我用于新操作按钮的活动类是MenuNotify。 – brettbrdls

+1

如果多个活动不会使用相同的菜单项,那么来自@ g2o的答案将使事情变得更简单,因为您确实不需要菜单项的基类。如果你知道如何使用调试器,在'onCreateOptionsMenu'中放置一个断点来查看它是否被调用过。否则[日志](http://developer.android.com/reference/android/util/Log.html)的东西logcat,看看你是否到达那里。根据你以前的评论,我仍然不确定你是否有类似'MainActivity extends MenuNotify'的地方,其中MainActivity是你开始的活动。 –

回答

1

我分开一个新的类,这是MenuNotify从我的主类我认为新的行动按钮,不知道这是在工具栏上添加新的动作按钮的正确方法。

没有必要创建新的类来添加新的操作按钮。 只需添加

<item 
    android:id="@+id/action_notify" 
    android:icon="@drawable/ic_notifications_black_24px" 
    android:title="@string/action_notify" 
    app:showAsAction="always" /> 

要在菜单/ main.xml中,并添加

case R.id.action_notify: 
      startActivity(new Intent(this, Home.class)); 
      return true; 

到主类onOptionsItemSelected方法的开关