gpt4 book ai didi

android - 使用抽屉导航和 fragment 未出现工具栏菜单

转载 作者:行者123 更新时间:2023-11-30 04:53:58 25 4
gpt4 key购买 nike

我正在使用抽屉导航和工具栏。当我尝试在 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);
}

list :

<?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>

解决方案:

发现错误!不要在 fragment 中再次执行 setSupportActionBar(toolbar) 。您只需在 Activity 中设置一次即可。

最佳答案

如果你想在 MapFragment 的父 Activity 上创建选项菜单

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

关于android - 使用抽屉导航和 fragment 未出现工具栏菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59539128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com