Android 展开的 SearchView 使其他 ActionBar 项目消失

Android expanded SearchView makes other ActionBar items disappear

我在片段调用的上下文操作栏中的 SearchView 遇到了一些问题。最主要的是,当我的 SearchView 展开时,它会使操作栏中的所有其他项目(关闭按钮除外)消失,即使有一些未使用的 space,如您在此屏幕截图中所见:

此外,我在这个问题中也遇到了同样的问题: ActionBar always expanded SearchView with the icon inside

这是我的 SearchView 的 XML 代码:

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

    <item android:id="@+id/menu_series_cab_search"
        android:icon="@drawable/ic_search"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

这是实现 ActionMode.Callback

的片段的 JAVA 代码
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.menu_series_cab, menu);
    mSearchView = (SearchView) MenuItemCompat
            .getActionView(menu.findItem(R.id.menu_series_cab_search));
    mSearchView.setQueryHint(getResources().getString(R.string.action_search));
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            mSearchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
    mSearchView.setIconifiedByDefault(false);
    return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    mSearchView.requestFocus();
    return true;
}

首先,搜索视图小部件具有最大固定大小,因此空 space 是不可避免的。

如果您希望 "action_settings" 项在搜索视图折叠时不消失,您需要设置 app:showAsAction="always"

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/menu_series_cab_search"
    android:icon="@drawable/ic_search"
    android:title="@string/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
    app:showAsAction="always" />

你的 Java 代码应该是这样的:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.main, menu);


    MenuItem searchItem = menu.findItem(R.id.action_search);
    // Get the SearchView and set the searchable configuration
    searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);//MenuItemCompat.getActionView(searchItem);//menu.findItem(R.id.action_search).getActionView();
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    super.onCreateOptionsMenu(menu, inflater);
}

感谢 link post 布朗克斯在他的最后 post 和其他像这样的回答 Android - cannot find TextView inside SearchWidget when using Android Support library (已接受的回答):在文本视图中移动放大图标,我从第一个布朗克斯答案中删除了这一行

searchView.setIconifiedByDefault(false);

然后我将其粘贴在 Bronx 代码之后:

    AutoCompleteTextView autoComplete = (AutoCompleteTextView)mSearchView.findViewById(R.id.search_src_text);
    Class<?> clazz = null;
    try {
        clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
        SpannableStringBuilder stopHint = new SpannableStringBuilder("  ");
        stopHint.append(getString(R.string.action_search));
        // Add the icon as an spannable
        Drawable searchIcon = getResources().getDrawable(R.drawable.abc_ic_search_api_mtrl_alpha);
        Method textSizeMethod = clazz.getMethod("getTextSize");
        Float rawTextSize = (Float)textSizeMethod.invoke(autoComplete);
        int textSize = (int) (rawTextSize * 1.25);
        searchIcon.setBounds(0, 0, textSize, textSize);
        stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // Set the new hint text
        Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
        setHintMethod.invoke(autoComplete, stopHint);
    } catch (Exception e) {
        // Set default hint
        mSearchView.setQueryHint(getResources().getString(R.string.action_search));
        e.printStackTrace();
    }

注意R.string.action_search是我项目中自定义的字符串,改成你的。再次感谢布朗克斯。