2016-01-28 43 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方法的開關