首页 > 解决方案 > 按钮打开错误的菜单

问题描述

我目前正在制作一个使用传统菜单按钮打开菜单的程序。有两个不同的按钮:第一个应该打开左侧菜单,第二个用于打开设置。问题是这两个按钮都打开了同一个菜单。请帮我做一些事情,以便每个按钮打开自己的菜单。

我已经使用有关这些按钮的 youtube 视频制作了应用程序的主要部分。但是他们有问题。

以下是左侧按钮打开的菜单截图:

打开左侧按钮的菜单

以下是应用程序的外观

应用程序的外观

以下是使用第二个按钮打开的同一菜单的快照

第二个按钮打开相同的菜单

这是我的代码:

package com.danielliakhovetskyi.mainactivity;

import android.content.ClipData;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {

    Menu menu;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    NavigationView navigationView;
    MenuItem maths;
    private boolean menuItemsAssigned = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Objects.requireNonNull(getSupportActionBar()).setBackgroundDrawable(new ColorDrawable
                (Color.parseColor("#872be3"))); //making ActionBar light-coloured
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.abs_layout);


        drawerLayout = findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView = findViewById(R.id.navview);
        navigationView.setItemTextAppearance(R.style.WithFont);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            Toast.makeText(MainActivity.this, "Default is clicked", Toast.LENGTH_SHORT).show();
            return super.onOptionsItemSelected(item);
        } else {
            return false;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.navigation_menu, menu);
       /* this.menu = menu;

        maths = menu.findItem(R.id.maths);
        Logger.getGlobal().log(Level.INFO, "Maths Clicked");
        Toast.makeText(this, "" + maths, Toast.LENGTH_SHORT).show();
        maths.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                maths.setTitle("Maths");
                Toast.makeText(MainActivity.this, "WORKS " + maths, Toast.LENGTH_SHORT).show();
                Logger.getGlobal().log(Level.INFO, "Maths Clicked");
                return false;
            }
        });
        menuItemsAssigned = true;*/
        return true;
    }

}

标签: javaandroid

解决方案


里面有onCreateOptionsMenu()这一行:

getMenuInflater().inflate(R.menu.navigation_menu, menu);

这显然是错误的,因为它将导航抽屉的菜单膨胀为操作栏菜单。
替换R.menu.navigation_menu为操作栏的菜单。


推荐阅读