0

我所取得的成就

我正在开发一个 Android 应用程序,该应用程序使用附加到我的 MainActivity 的导航抽屉,以在用户选择导航项时显示某些布局。我还为应该出现在 appBar 中的溢出菜单创建了一个菜单资源文件。

我无法实现的目标

但是,无论用户选择的导航抽屉项目如何,我创建的溢出菜单都会显示在 appBar 中。如果可能,我需要在显示布局日历/设置时从 appBar 隐藏此溢出菜单。(代码如下)

我试图做的事

我尝试使用该setHasOptionsMenu()方法并将其设置为 false 在我的日历/设置 Java 文件中。

申请代码

主要活动

package com.example.lukeb.calendar;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
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.support.v7.widget.Toolbar;
import android.text.Html;
import android.transition.Visibility;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    // Declare needed objects as global variables for access throughout the program

    RelativeLayout lay1;
    RelativeLayout lay2;
    RelativeLayout lay3;
    DrawerLayout drawerLayout;
    ActionBar actionBar;
    Toolbar toolbar;

    // Default method called automatically when activity is created

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);         // Set the content of this activity to the activity_main XML file
        navigate();                                     // Once finished with this, call the navigate function found below
    }

    // Method created to prep navigation drawer

    public void navigate(){                                 // Method called in onCreate method

        // If device is running Lollipop or above, run the following...
        if(Build.VERSION.SDK_INT >= 21) {
            toolbar = (Toolbar) findViewById(R.id.toolbar3);            // Set variable "toolbar" to item named "toolbar3" in XML file
            setSupportActionBar(toolbar);                               // Designate the item set to "toolbar" as the activity's actionBar
            actionBar = getSupportActionBar();                          // Set variable "actionBar" to whatever the activity's actionBar is. This is necessary for checking it later.
            lay1 = (RelativeLayout) findViewById(R.id.one);             // Initialize variables "lay1", "lay2", and "lay3" - that were originally declared above - to their respective XML items.
            lay2 = (RelativeLayout) findViewById(R.id.two);             //    < __________________________|           |
            lay3 = (RelativeLayout) findViewById(R.id.three);           //    < ______________________________________|


            drawerLayout = (DrawerLayout) findViewById(R.id.dLayout);   // Initialize variable "drawerLayout" to XML item "dLayout".
            if (actionBar != null) {                                    // Check if the activity's actionBar has been set (not equal to null)
                actionBar.setDisplayHomeAsUpEnabled(true);              // Enables the actionBar's back button
                ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {       // Create a new AppDrawer toggle. Refer to android documentation for more info on parameters. Also defines its own constructor after the class object.
                    public void onDrawerClosed(View view) {  // Method called when drawer is closed
                        supportInvalidateOptionsMenu();      // Declare that the options menu has changed, so should be recreated. It will be recreated when needed.

                    }

                    public void onDrawerOpened(View drawerView){    // Method called when drawer is opened
                        supportInvalidateOptionsMenu();             // Once again, declare that the options menu has changed, so should be recreated. It will be recreated when needed.

                    }
                };
                mDrawerToggle.setDrawerIndicatorEnabled(true);      // Enable drawer indicator on the "mDrawerToggle" which, in turn, enables the animated glyph on the activity's actionBar
                drawerLayout.setDrawerListener(mDrawerToggle);      // Set the appDrawer's actionListener to our newly created "mDrawerToggle" object
                mDrawerToggle.syncState();                          // Sync the appDrawer's indicator with the current state of the DrawerLayout
                setListener();                                      // Call the setListener method below

            }
        }
    }

    // Sets the item listener
    public void setListener(){
        NavigationView nView = (NavigationView) findViewById(R.id.nav_view);    // Declare a variable named "nView" and initialize it to the "nav_view" XML item.
        nView.setNavigationItemSelectedListener(this);                          // Set the listener called for a selected navDrawer item to one found below
    }


    // A method that is called when a navDrawer item is selected
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){                                               // Check which item was selected by returning its ID
            case R.id.action_feed:                                              // If the item selected's ID is "R.id.action_feed"
                actionBar.setTitle("Your Feed");                                // Set actionBar title to correct page title
                lay3.setVisibility(View.GONE);                                  // Hide layout3 from the "stack of layouts"
                lay2.setVisibility(View.GONE);                                  // Hide layout2 from the "stack of layouts"
                lay1.setVisibility(View.VISIBLE);                               // Show only the selected item on screen

                break;                                                          // Leave or "break" from the loop

            case R.id.action_calendar:                                          // If the item selected's ID is "R.id.action_calendar"
                actionBar.setTitle("Calendar");                                 // Set actionBar title to correct page title
                lay1.setVisibility(View.GONE);                                  // Hide layout1 from the "stack of layouts"
                lay3.setVisibility(View.GONE);                                  // Hide layout3 from the "stack of layouts"
                lay2.setVisibility(View.VISIBLE);                               // Show only the selected item on screen
                break;                                                          // Leave or "break" from the loop

            case R.id.action_settings:                                          // If the item selected's ID is "R.id.action_settings"
                actionBar.setTitle("Settings");                                 // Set actionBar title to correct page title
                lay1.setVisibility(View.GONE);                                  // Hide layout1 from the "stack of layouts"
                lay2.setVisibility(View.GONE);                                  // Hide layout3 from the "stack of layouts"
                lay3.setVisibility(View.VISIBLE);                               // Show only the selected item on screen
                break;                                                          // Leave or "break" from the loop

        }
        drawerLayout.closeDrawer(GravityCompat.START);                          // Regardless of selection, close drawer when loaded
        return true;                                                            // Return true if navDrawer item was indeed selected
    }

    public boolean newEvent(MenuItem item){
        Intent i = new Intent(MainActivity.this, NewEvent.class);
        startActivity(i);
        return true;
    }
}

FragOne

package com.example.lukeb.calendar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragOne extends Fragment {

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

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        // Creates a new view for the fragment. Then, it sets the XML layout file as the contents of the view. Finally, a copy of this new view is returned.

        return inflater.inflate(R.layout.fragment_frag_one, container, false);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        //inflater = getMenuInflater();
        inflater.inflate(R.menu.overflow_one, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

}

片段二

package com.example.lukeb.calendar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragTwo extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(false);
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        // Creates a new view for the fragment. Then, it sets the XML layout file as the contents of the view. Finally, a copy of this new view is returned.

        return inflater.inflate(R.layout.fragment_frag_two, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
    }
}
4

1 回答 1

0

我知道这个问题已经很老了,但是我最近一直在尝试隐藏溢出菜单,并认为这可能对查看此问题的任何人都有益,以查看我的解决方案。


你是对的setHasOptionsMenu(),但是,我认为你对它的作用有点困惑。根据文档,此方法只是告诉活动它想要填充选项菜单。我已经在下面发布了我的解决方案,希望它对您有所帮助,如果您不明白,请随时提出任何问题。


我的第一步是编辑我的菜单资源文件以将所有项目添加到一个组中。我的现在看起来像这样:

<group
    android:id="@+id/main_menu_group">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</group>

然后我像这样覆盖该onPrepareOptionsMenu()方法:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    Log.d("Method Calls" , "onPrepareMenuOptions");
    menu.setGroupVisible(R.id.main_menu_group, false);
    super.onPrepareOptionsMenu(menu);
}

此代码删除了之前创建的组中所有项目的可见性,因此,不需要选项菜单,因此 android 不会显示它。

这段代码还没有做任何事情,因为选项菜单已经由活动创建,setHasOptionsMenu(true);在片段onCreate()方法中使用将通过告诉活动它想要进行一些更改来解决这个问题。

要在另一个片段中添加溢出菜单,您可以在该片段中重复最后两个步骤,但要更改: menu.setGroupVisible(R.id.main_menu_group, false);

至: menu.setGroupVisible(R.id.main_menu_group, true);

于 2018-05-17T18:02:47.573 回答