在操作栏中隐藏SubMenu和SearchView

时间:2015-05-14 09:05:18

标签: android android-actionbar

我试图隐藏ActionBar中的SubMenu和SearchView。我正在使用ActionBarSherlock。

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Used to put dark icons on light action bar
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;         

    //Create the search view
    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Zong ol na...");
    searchView.setOnQueryTextListener(this);
    searchView.setOnSuggestionListener(this);

    searchView.setSuggestionsAdapter(mSuggestionsAdapter);

    SubMenu sub = menu.addSubMenu("Menu");
    sub.add(0, R.style.Theme_Sherlock, 0, "Topic");
    sub.add(0, R.style.Theme_Sherlock_Light, 0, "Thuthlunghlui");        
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

我想点击按钮

隐藏SubMenu和SearchView
Btn_Hide_Menu.setOnClickListener(new OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        { //here I want to hide both the SubMenu and SearchView

我无法自己解决这个问题。这可能吗?对不起,我太天真了。 enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个希望它有效

    Btn_Hide_Menu.setOnClickListener(new OnClickListener() 
        {           
            @Override
            public void onClick(View v) 
            { //here I want to hide both the SubMenu and SearchView
                MenuItem item = menu.findItem(R.id.my_item);
                item.setVisible(false);    
            }    
     }

答案 1 :(得分:1)

如果您在此{/ 1}}文件中有菜单

menu_main.xml

在您的活动类中声明一些全局变量

<menu 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">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/tanim"
        android:title="Books"
        android:orderInCategory="100"
        app:showAsAction="never" />    

</menu>

现在查看您的private Menu menu; private MenuItem item; private MenuItem item1;

onOptionCreateMenu()

最后你的按钮点击事件,野兔项目,item1是全局变量..

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
             item = menu.findItem(R.id.tanim);// hare tanim is menu id comes from xml file..

            menu.add("search"); // this menu add hare no xml id ... so you need the index number of this menu  
            // index is 0=search,1=settings,2=tanim  

            item1=menu.getItem(0);// this is search menu
             return true;
        }

随时提出任何问题......谢谢