取消隐藏操作栏项目

取消隐藏操作栏项目

本文介绍了在状态隐藏/取消隐藏操作栏项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我都在操作栏的三个图标(搜索 - 加 - 计数),我想,当变量 hideIcon 来隐藏这些图标。

以下是我的code。

MainActivity.java

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions,菜单);

SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    搜索查看搜索查看=(搜索查看)menu.findItem(R.id.action_search)
            .getActionView();
    sea​​rchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));

    返回super.onCreateOptionsMenu(菜单);
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //将每个动作项目点击相应的操作
    开关(item.getItemId()){
    案例R.id.action_search:
        //搜索行动
        返回true;

    案例R.id.action_add_tip:

        返回true;

    案例R.id.action_count_tip:

        返回true;

    案例android.R.id.home:

        返回true;

    默认:

        返回super.onOptionsItemSelected(项目);
    }
}
 

menu.activity_main_actions.xml

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

<! - 搜索 - >
<项目机器人:ID =@ + ID / ACTION_SEARCH
      机器人:图标=@可绘制/ header_search
      机器人:标题=@字符串/ ACTION_SEARCH
      机器人:showAsAction =ifRoom
      机器人:actionViewClass =android.widget.SearchView/>


<! - 添加提示 - >
<项目机器人:ID =@ + ID / action_add_tip
      机器人:图标=@可绘制/ header_add
      机器人:标题=@字符串/ action_add_tip
      机器人:showAsAction =ifRoom/>

<! - 计数技巧 - >
<项目机器人:ID =@ + ID / action_count_tip
      机器人:图标=@可绘制/ header_count
      机器人:标题=@字符串/ action_count_tip
      机器人:showAsAction =ifRoom/>

< /菜单>
 

我怎么能隐藏它们如果(hideIcon)?先谢谢了。

解决方案

  //在code的任何地方

hideIcon = HIDE_MENU; //设置状态

invalidateOptionsMenu(); //现在onCreateOptionsMenu(...)再次调用




 @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    //膨胀从XML菜单
    MenuInflater充气= getSupportMenuInflater();
    inflater.inflate(R.menu.settings,菜单);

    如果(hideIcon)
    {
        的for(int i = 0; I< menu.size();我++)
            menu.getItem(ⅰ).setVisible(假);
    }
}
 

In my Activity I have an three icons in Action bar (search - add - count), I want when variable hideIcon is true to hide these icons.

The following is my code.

MainActivity.java

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions, menu);

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Take appropriate action for each action item click
    switch (item.getItemId()) {
    case R.id.action_search:
        // search action
        return true;

    case R.id.action_add_tip:

        return true;

    case R.id.action_count_tip:

        return true;

    case android.R.id.home:

        return true;

    default:

        return super.onOptionsItemSelected(item);
    }
}

menu.activity_main_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Search  -->
<item android:id="@+id/action_search"
      android:icon="@drawable/header_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom"
      android:actionViewClass="android.widget.SearchView"/>


<!-- Add Tip -->
<item android:id="@+id/action_add_tip"
      android:icon="@drawable/header_add"
      android:title="@string/action_add_tip"
      android:showAsAction="ifRoom" />

<!-- Count Tips -->
<item android:id="@+id/action_count_tip"
      android:icon="@drawable/header_count"
      android:title="@string/action_count_tip"
      android:showAsAction="ifRoom" />

</menu>

How can I hide them if(hideIcon) is true ?Thanks in advance.

解决方案
//anywhere in your code

hideIcon = HIDE_MENU; // setting state

invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again




 @Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // inflate menu from xml
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.settings, menu);

    if (hideIcon)
    {
        for (int i = 0; i < menu.size(); i++)
            menu.getItem(i).setVisible(false);
    }
}

这篇关于在状态隐藏/取消隐藏操作栏项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:59