java - 按钮打开错误的菜单
问题描述
我目前正在制作一个使用传统菜单按钮打开菜单的程序。有两个不同的按钮:第一个应该打开左侧菜单,第二个用于打开设置。问题是这两个按钮都打开了同一个菜单。请帮我做一些事情,以便每个按钮打开自己的菜单。
我已经使用有关这些按钮的 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;
}
}
解决方案
里面有onCreateOptionsMenu()
这一行:
getMenuInflater().inflate(R.menu.navigation_menu, menu);
这显然是错误的,因为它将导航抽屉的菜单膨胀为操作栏菜单。
替换R.menu.navigation_menu
为操作栏的菜单。
推荐阅读
- html - Chrome:我的视频在访问页面时开始自动播放,但在重新加载页面时没有
- orbeon - orbeon表单问题
- python - 将 2 个 Pandas 列表填充列连接成 1 个大列表?
- python - Why is r' still duplicating the forward slashes in my code?
- c++ - 缺少 QLayout 构造函数来实现卡片布局
- plot - Finding a quantity of anything between two points in space
- python - How to convert column into category 'as_known()' with dask dataframe?
- jms - 使用 Spring Batch 和 Spring Integration 在 master 的同时关闭 worker 的好习惯
- excel - Excel/SQL,在命名范围内排除最后一个“,”
- python - Append values to dict in class inheritance