问题:我正在使用自定义工具栏。当我单击溢出图标按钮时,它将打开菜单项,但它与我的导航抽屉分开。例如,如果我打开导航抽屉,然后单击工具栏中的溢出图标,它将再次在导航抽屉上打开菜单。
我尝试过的事情:我已经尝试过遍历MainActivity.XML中视图的结构,但是似乎没有任何改变。我还向mMenuToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close);
添加了工具栏,但是只是在工具栏左侧创建了一个新的汉堡图标,当我单击它时,它使应用程序崩溃。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolBar"
layout="@layout/toolbar"/>
<android.support.v4.widget.DrawerLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/main_activity_drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/wf_logo_text"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu="@menu/navigation_menu"
android:fitsSystemWindows="true"
android:layout_gravity="end">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
toolbar.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
android:id="@+id/toolBar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/wf_logo_text"
android:layout_gravity="center"/>
</android.support.v7.widget.Toolbar>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:checkableBehavior="single">
<item android:id="@+id/locations"
android:title="ATMs/Locations"
app:showAsAction="never"
android:checkable="false" />
</group>
</menu>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mMenuToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sets toolbar
toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
Drawable drawable =
ContextCompat.getDrawable(getApplicationContext(),
R.drawable.ic_menu_white_24dp);
toolbar.setOverflowIcon(drawable);
// Main Activity DrawerLayout with Toggle
mDrawerLayout = findViewById(R.id.main_activity_drawer_layout);
// Menu Toggle Button
mMenuToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mMenuToggle);
mMenuToggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return true;
}
}
发生的事情和我想发生的事情:我只想使用在右侧的自定义工具栏上创建的同一个溢出图标,并能够单击它并拥有它打开导航抽屉,而不是单独的菜单。