如何使搜索菜单项来操作栏采用了ABS的在众目睽睽采用了、众目睽睽、菜单项、操作

2023-09-12 02:12:46 作者:粉雾海岸

我的行动吧,这我使用的是行动起来吧福尔摩斯库五个行动菜单项如下:

I have five action menu items in the action bar, which I'm using action bar sherlock library as follows :

onCreateOptionsMenu(),我用下面的code:

In onCreateOptionsMenu(), i used the following code :

  menu.add(0,1,0 "Settings")
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

  menu.add(0,2,0 "Favorites")
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

  menu.add(0,3,0 "List")
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

  menu.add(0,4,0 "Upload")
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

  menu.add(0,5,0 "Search")
     .setActionView(R.layout.search_layout)
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

现在我的问题是搜索编辑文本(这是红色)出现这样的:

Now my Problem is Search Edit text (which is in red color) appears like this:

我要让它在操作栏全貌,是这样的:

I want to make it to a full view in the action bar, like this :

推荐答案

这个问题是有点老,但我会尽量尝试回答。我希望有人会发现它是有用的。

This question is a bit old but I'll try to answer it anyway. I hope someone will find it useful.

下面是我做了什么:

创建扩展搜索查看自定义SearchView类。它定义了两个事件:一是发射时,搜索视图扩展,另外,当它崩溃

Create a custom SearchView class that extends SearchView. It defines 2 events: one fired when the search view expands and the other when it collapses.

public class EnglishVerbSearchView extends SearchView {

OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;

public EnglishVerbSearchView(Context context) {
    super(context);
}

@Override
public void onActionViewCollapsed() {
    if (mSearchViewCollapsedEventListener != null)
        mSearchViewCollapsedEventListener.onSearchViewCollapsed();
    super.onActionViewCollapsed();
}

@Override
public void onActionViewExpanded() {
    if (mOnSearchViewExpandedEventListener != null)
        mOnSearchViewExpandedEventListener.onSearchViewExpanded();
    super.onActionViewExpanded();
}

public interface OnSearchViewCollapsedEventListener {
    public void onSearchViewCollapsed();
}

public interface OnSearchViewExpandedEventListener {
    public void onSearchViewExpanded();
}

public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
    mSearchViewCollapsedEventListener = eventListener;
}

public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
    mOnSearchViewExpandedEventListener = eventListener;
}

}

在我的活动,我做了以下内容: - 创建一个私有字段存储的参考菜单。 - 定义的折叠和展开,我隐藏和显示其他操作使用参照菜单中的搜索视图的事件。

In my activity, I did the following: - Created a private field to store the reference to the menu. - Defined the events for collapsing and expanding the search view where I hide and show other actions by using the reference to the menu.

public class DictionaryActivity extends ActionBarActivity {

    private Menu mMenu;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.dictionary, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    EnglishVerbSearchView searchView = (EnglishVerbSearchView) MenuItemCompat.getActionView(searchItem);        

    searchView.setOnSearchViewCollapsedEventListener(listenerCollapse);
    searchView.setOnSearchViewExpandedEventListener(listenerExpand);

    mMenu = menu;

    return super.onCreateOptionsMenu(menu);
}

final private OnSearchViewCollapsedEventListener listenerCollapse = new OnSearchViewCollapsedEventListener() {

    @Override
    public void onSearchViewCollapsed() {
        // show other actions
        MenuItem favouriteItem = mMenu.findItem(R.id.action_favourite);
        MenuItem playItem = mMenu.findItem(R.id.action_play);
        favouriteItem.setVisible(true);
        playItem.setVisible(true);

        // I'm doing my actual search here          
    }
};

final private OnSearchViewExpandedEventListener listenerExpand = new OnSearchViewExpandedEventListener() {

    @Override
    public void onSearchViewExpanded() {
        // hide other actions
        MenuItem favouriteItem = mMenu.findItem(R.id.action_favourite);
        MenuItem playItem = mMenu.findItem(R.id.action_play);
        favouriteItem.setVisible(false);
        playItem.setVisible(false);
    }
};

}

在menu.xml文件的文件,我使用的自定义搜索视图:

In the menu.xml file, I used the custom search view:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/action_search"
      yourapp:showAsAction="always|collapseActionView"
      yourapp:actionViewClass="com.xxx.EnglishVerbSearchView" />
</menu>

我希望这是所有的需要,因为它只是从活动中我充分code的提取物。让我知道如果有什么遗漏。

I hope it's all that's needed as it's just an extract from my full code of the activity. Let me know if there's anything missing.

根据从arne.jans评论:

As per comment from arne.jans:

这似乎是足够的做 MenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 来腾出空间给搜索查看。其优点是:其他菜单项进入溢流菜单,仍然可以访问,即使在打开搜索查看

It seems to be enough to do MenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); to make room for the SearchView. The advantage would be: the other menu-items go into the overflow menu and are still accessible, even with the opened SearchView.