14

在此处输入图像描述

我试图在我的应用程序中创建一个操作栏搜索,但在展开状态下,SearchView没有占用整个操作栏宽度(它仍然显示其他操作)!

那么,如何让 SearchView 填满整个 ActionBar 区域(就像在 GMAIL 应用中一样)?

4

5 回答 5

29

以上解决方案都不适合我。设置 SearchView 的 MaxWidth 对我有用:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
    searchView.setMaxWidth(Integer.MAX_VALUE);
于 2015-12-02T19:09:02.113 回答
11

知道了

在此处输入图像描述

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

    MenuItem searchViewItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchViewItem.getActionView();
    searchView.setIconifiedByDefault(false);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    searchView.setLayoutParams(params);
    searchViewItem.expandActionView();
    return true;
}


<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=".MySwipeTabbedActivity" >
    <item android:id="@+id/action_search"
            android:title="Search"
            android:icon="@android:drawable/ic_menu_search"
            android:showAsAction="always|collapseActionView"
            android:actionViewClass="android.widget.SearchView"
            />
    <item android:id="@+id/action_share"
            android:title="Share"
            android:icon="@android:drawable/ic_menu_share"
            android:showAsAction="ifRoom" />
    <item android:id="@+id/action_settings"
            android:title="Settings"
            android:icon="@android:drawable/ic_menu_preferences"
            android:showAsAction="never" />
</menu>

[可选] 避免 searchView 折叠:

    searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return false; //never collapse
        }
    });
于 2014-08-01T23:21:27.653 回答
2

@Hiep给出的答案是正确的应用紧凑型

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

<item android:id="@+id/action_search"
      android:title="search"
      android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
      materialdesign:showAsAction="always|collapseActionView"
      materialdesign:actionViewClass="android.support.v7.widget.SearchView" />

</menu>

并在主类onCreateOptionsMenu

  @Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);



    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));


    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    searchView.setLayoutParams(params);
    searchView.setIconified(false);

     MenuItemCompat.expandActionView(searchItem);

   return super.onCreateOptionsMenu(menu);
}

避免 searchView 崩溃:如果您在 Appcompact 中使用上述方法,则会造成崩溃,因此请使用此解决方案来避免这种情况。

      MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {

        /* (non-Javadoc)
         * @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
         */
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {

            return true;
        }

        /* (non-Javadoc)
         * @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
         */
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {

            return false;
        }
    });

谢谢

于 2015-01-08T17:28:06.063 回答
1

Try This Only 将起作用并将所有其他项目设置为 ifRoom

<item
    android:id="@+id/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="@string/action_search"
    app:showAsAction="always"/>
于 2017-02-28T07:21:56.533 回答
0

是的,我真的迟到了 :) 但我想再分享一个替代解决方案。这个想法只是在SearchView扩展时隐藏分组的菜单项。

第 1 步: 对菜单项进行分组。请确保showAsAction搜索视图项目是app:showAsAction="collapseActionView|always"

<group android:id="@+id/myMenuGroup">
    <item
        android:id="@+id/actionSearch"
        android:icon="@drawable/ic_search"
        android:title="@string/search"
        app:actionViewClass="android.widget.SearchView"
        app:iconTint="@color/textColorVariant2"
        app:showAsAction="collapseActionView|always" />

    <item
        android:id="@+id/actionFilter"
        android:icon="@drawable/ic_filter"
        android:orderInCategory="0"
        android:title="@string/filter"
        app:iconTint="@color/textColorVariant2"
        app:showAsAction="ifRoom" />

        ..... </group>

第二步:onPrepareOptionsMenu()在回调中获取菜单引用

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
    super.onPrepareOptionsMenu(menu)
    this.menu = menu // this.menu is a global scoped variable
    return true
}

第 3 步 添加MenuItem.OnActionExpandListener到您的活动并实施onMenuItemActionExpand()onMenuItemActionExpand()如下所示

override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
    menu?.setGroupVisible(R.id.myMenuGroup, false)
    return true
}

override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
    menu?.setGroupVisible(R.id.myMenuGroup, true)
    return true
}

即使存在其他图标,这种方法也会完全扩展SearchView内部ActionBarmenu

在此处输入图像描述

在此处输入图像描述

于 2020-05-08T12:58:54.223 回答