我正在使用导航抽屉和工具栏。当我尝试在DrawerActivity的工具栏上创建选项菜单时,未显示。我浏览了很多在线资源,但仍然找不到解决方案。有人知道为什么吗?
menu_advance.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_edit_marker"
android:orderInCategory="101"
android:title="@string/menu_edit_marker"
app:showAsAction="never"/>
<item
android:id="@+id/menu_config_mode"
android:orderInCategory="103"
android:title="@string/reconfig_mode_on"
app:showAsAction="never"/>
<item
android:id="@+id/menu_installation_mode"
android:orderInCategory="104"
android:title="@string/installation_mode_on"
app:showAsAction="never"/>
</menu>
activity_drawer.xml :
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/drawer_activity_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/ic_menu_white_24dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
DrawerActivity.java :
public class DrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer_layout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
Toolbar toolbar = findViewById(R.id.drawer_activity_toolbar);
setSupportActionBar(toolbar);
drawer_layout = findViewById(R.id.drawer_layout);
NavigationView navigation_view = findViewById(R.id.nav_view);
navigation_view.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer_layout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer_layout.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment(), "MAP FRAGMENT").commit();
navigation_view.setCheckedItem(R.id.office_map);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_advance, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_config_mode:
Toast.makeText(this, "config", Toast.LENGTH_SHORT).show();
return true;
....
}
return super.onOptionsItemSelected(item);
}
MapFragment.java :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
setHasOptionsMenu(true);
toolbar = view.findViewById(R.id.drawer_activity_toolbar);
((AppCompatActivity) requireActivity()).setSupportActionBar(toolbar);
....
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_advance, menu);
optionMenu = menu;
super.onCreateOptionsMenu(menu, inflater);
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.example.vlcips">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".DrawerActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<dist:module dist:instant="true" />
</manifest>
编辑:
发现错误!不要在片段中再次执行setSupportActionBar(toolbar)。您只需在活动中设置一次即可。
答案 0 :(得分:0)
如果要在MapFragment的父活动上创建选项菜单
MapFragment.java:
@Override
public View onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_advance, menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
// handle menu item clicks here
return super.onOptionsItemSelected(item);
}
无需重写DrawerActivity.java中的onCreateOptionsMenu。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
Toolbar toolbar = findViewById(R.id.drawer_activity_toolbar);
setSupportActionBar(toolbar);
.....
}