菜单项未出现在工具栏中

huangapple go评论88阅读模式
英文:

MenuItem doesn't appear in toolbar

问题

我面临一个问题。我无法让工具栏显示菜单。我知道这个问题之前在这里被问过,在这里,我已经尝试了所有可能的解决方案,但没有一个给我想要的结果。所以我正在使用1个活动和几个片段,我希望根据片段在工具栏中显示不同的菜单项。这是我的 MainActivity.java 代码:

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    // ... 这里是你的其他代码 ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ... 这里是你的其他初始化代码 ...

        // 在这里尝试通过调用 toolbar.inflateMenu(R.menu.profile_fragment_menu) 来设置菜单
        toolbar.inflateMenu(R.menu.profile_fragment_menu);

        // ... 这里是你的其他代码 ...
    }

    // ... 这里是你的其他方法 ...
}

MainActivityonCreate 方法中,我尝试在适当的位置使用 toolbar.inflateMenu(R.menu.profile_fragment_menu) 来设置菜单。

这是你的 ProfileFragment.java 代码:

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class ProfileFragment extends Fragment {
    // ... 这里是你的其他代码 ...

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true); // 告诉 Fragment 有菜单项
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // ... 这里是你的其他初始化代码 ...
        return root;
    }

    @Override
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
        inflater.inflate(R.menu.profile_fragment_menu, menu); // 设置菜单
    }
}

ProfileFragment.javaonCreate 方法中,你使用了 setHasOptionsMenu(true) 来指示该片段应该拥有菜单项。然后,在 onCreateOptionsMenu 方法中,你使用 inflater.inflate(R.menu.profile_fragment_menu, menu) 来设置菜单。

通过这些更改,你应该能够在 ProfileFragment 中的工具栏中显示相应的菜单项。

英文:

I'm facing a problem. I can't make a toolbar to show me a menu. I know that this question was asked here previously here, and I went through all possible solutions, but none of them gave me desired result. So I'm using 1 activity and few fragments and I want to show different menu items in a toolbar according to fragment. Here is my MainActivity.java

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    DrawerLayout drawerLayout;
    NavigationView drawerView;
    NavController navController;
    AppBarConfiguration appBarConfiguration;
    ConstraintLayout container;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        drawerLayout = findViewById(R.id.drawer_layout);
        drawerView = findViewById(R.id.drawer_view);
        toolbar = findViewById(R.id.toolbar);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        container = findViewById(R.id.container);

        drawerView.bringToFront();
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.nav_host_fragment);

        navController = navHostFragment.getNavController();
        //NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_main, R.id.navigation_favourite,
                R.id.navigation_add_announce,R.id.navigation_notifications,
                R.id.navigation_profile).setOpenableLayout(drawerLayout).build();

        NavigationUI.setupWithNavController(drawerView, navController);
        NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        drawerView.setCheckedItem(R.id.drawer_main);
        drawerView.setNavigationItemSelectedListener(this);
        animateDrawer();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.profile_fragment_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.onNavDestinationSelected(item, navController)
                || super.onOptionsItemSelected(item);
    }


And i want to show LogIn text in ProfileFragment toolbar
ProfileFragment.java code:


import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


public class ProfileFragment extends Fragment {

    ProfileViewModel mViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(ProfileViewModel.class);
        View root = inflater.inflate(R.layout.profile_fragment, container, false);



        return root;
    }

    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
        inflater.inflate(R.menu.profile_fragment_menu, menu);
    }


}

profile_fragment_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">

    <item
        android:id="@+id/registerFragment"
        android:title="@string/enter_acc"<!-- LogIn -->
        app:showAsAction="always"/>

</menu>

mobile_navigation.xml holds navigation graph

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/navigation_main">

    <fragment
        android:id="@+id/navigation_main"
        android:name=".ui.main.MainFragment"
        android:label="@string/nav_bar_main"
        tools:layout="@layout/main_fragment" />

    <fragment
        android:id="@+id/navigation_favourite"
        android:name=".ui.favourite.FavouriteFragment"
        android:label="@string/nav_bar_favourite"
        tools:layout="@layout/favourite_fragment" />

    <fragment
        android:id="@+id/navigation_add_announce"
        android:name=".ui.add.AddFragment"
        android:label="@string/nav_bar_add_announce"
        tools:layout="@layout/add_fragment" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name=".ui.notifications.NotificationsFragment"
        android:label="@string/nav_bar_notifications"
        tools:layout="@layout/notifications_fragment" />

    <fragment
        android:id="@+id/navigation_profile"
        android:name=".ui.profile.ProfileFragment"
        android:label="@string/nav_bar_profile"
        tools:layout="@layout/profile_fragment" >
        <action
            android:id="@+id/action_navigation_profile_to_registerFragment"
            app:destination="@id/registerFragment" />
    </fragment>
    <fragment
        android:id="@+id/registerFragment"
        android:name=".ui.profile.register.RegisterFragment"
        android:label="register_fragment"
        tools:layout="@layout/register_fragment" />
</navigation>

I tried toolbar.inflateMenu(R.menu.profile_fragment_menu) in MainActivity within onCreate method
and menu item appeared and was clickable but it was showing on all fragments + onOptionsItemSelected didn't process click on item. My last try you can see in my code. Thank you in advance

答案1

得分: 0

如果您想将 Toolbar 用作 ActionBar 并使用 ActionBar API(例如 onCreateOptionsMenu),您需要添加 setSupportActionBar(toolbar);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

否则,如果您不调用 setSupportActionBar,您可以直接使用 Toolbar API,并在每个片段中使用 inflateMenu() 方法。类似于:

toolbar.inflateMenu(R.menu.xxxxx);

然后使用 setOnMenuItemClickListener 方法设置点击监听器。

英文:

If you want to use the Toolbar as ActionBar and use the ActionBar API (as onCreateOptionsMenu) you have to add setSupportActionBar(toolbar);

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

Otherwise if you don't call setSupportActionBar you can use the Toolbar APIs directly and use the method inflateMenu() in each fragment. Something like:

toolbar.inflateMenu(R.menu.xxxxx)

and then use the setOnMenuItemClickListener method to set a clickListener.

huangapple
  • 本文由 发表于 2020年10月17日 00:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定