简体   繁体   中英

Added action button on the toolbar isn't showing up

So I recently trying to add another action button beside the overflow icon on the toolbar:

在此处输入图片说明

But by following a tutorial on adding action buttons, I can't get it to show on my toolbar.

Here's my menu/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>

I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.

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);
        }
    }
}

Btw, my filter icon button is in my main class.

EDIT: Thanks g2o for the help!

在此处输入图片说明

I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.

There is no need to create new class to add new action button. Just add

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

To your menu/main.xml and add

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

to the switch of your Main class onOptionsItemSelected method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM