Android : 在 SearchView 中最小化 space

Android : Minimize space in SearchView

我试图从后退箭头和 searchview 组件之间的 space 位置了解。我使用单独的 activity 通过工具提示进行搜索。

我尝试将填充 0 添加到搜索视图,但没有成功。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/search_toolbar_color"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:padding="0dp"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/search_results"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:visibility="gone" />

    </RelativeLayout>
</LinearLayout>

我创建了自定义搜索视图以满足我的要求,输入搜索时打开键盘 activity,删除搜索图标并更改 color 方案

  @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);

        SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
        if (searchItem != null)
        {
            _searchView = (SearchView) searchItem.getActionView();
        }

        if (_searchView != null)
        {
            _searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
            _searchView.setQueryHint(getString(R.string.action_search));
            _searchView.setIconifiedByDefault(false);
            final SearchView finalSearchView = _searchView;
            _searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
            {
                @Override
                public void onFocusChange(View view, boolean hasFocus)
                {
                    if (hasFocus)
                    {
                        //                      showInputMethod(view);
                        finalSearchView.onActionViewExpanded();
                    }
                }
            });


            //remove the icon in searchView
            ImageView searchViewIcon = (ImageView) _searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
            ViewGroup linearLayoutSearchView = (ViewGroup) searchViewIcon.getParent();
            linearLayoutSearchView.removeView(searchViewIcon);

            _searchView.setOnQueryTextListener(this);
            _searchView.requestFocus();

        }
        return true;
    }

更新:

在使用视图层次结构检查工具栏布局后,我开始意识到在向后箭头图像和搜索视图之间存在某种应用程序兼容性文本视图。不幸的是,我仍然不知道如何从那里删除它。

您可以在 Toolbar 中设置这些属性以最小化额外的空间(可以为边距设置负值):

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    xmlns:app="schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/search_toolbar_color"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp" />

原回答:

或者,您可以使用负边距的 SearchBar 组件:

<android.support.v7.widget.SearchView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="-16dp"
  android:paddingStart="-16dp"
  app:iconifiedByDefault="false"
  app:searchIcon="@null"
  app:queryHint="Search"
  app:theme="@style/ThemeOverlay.AppCompat.Dark" />

SearchBar documentation

我找到了适合我的解决方案,我添加了这一行

_searchView.setMaxWidth(Integer.MAX_VALUE); 

在扩充菜单布局后在 onCreateOptionsMenu 中。

您可以在此处阅读更多内容 [link][1]

我还使用

从操作栏中删除了标题
getSupportActionBar().setDisplayShowTitleEnabled(false);

(*) 箭头和搜索视图之间仍然有一个 space,但它要小得多,我可以接受。

(**) GuilhermeFGL,很遗憾,您提供的解决方案对我不起作用,可能是因为我是 运行 牛轧糖。但是谢谢你的帮助。