按钮打开错误的菜单

时间:2019-03-25 18:37:29

标签: java android

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

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

以下是使用左侧按钮打开的菜单的快照:

Menu that opens left side button

以下是该应用的外观

The look of the app

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

The second button opens the same menu

这是我的代码:

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

}

1 个答案:

答案 0 :(得分:1)

onCreateOptionsMenu()中有以下行:

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

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