且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 搜索视图建议填充正确

更新时间:2022-12-26 20:07:09

只有这个对我有用:

@Override
public boolean onQueryTextChange(String query) {
    mSearchSrcTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    mSearchSrcTextView.setDropDownAnchor(((View) getParent()).getId());
    ... 
}

您必须设置具有所需宽度的锚定视图.否则下拉列表不会变得比输入字段更宽.我只使用 searchView 的父级.

You must set an anchor view that has desired width. Else dropdown will not become wider than input field. I just use the parent of searchView.

正如@stav-bodik 提到的:它需要在每个 onQueryTextChange 上调用.如果你只做一次,它似乎不起作用.(不需要更改背景)

As @stav-bodik mentioned: it needs to be called on every onQueryTextChange. It seems not to work if you do it only once. (background change is not needed)

您还应该持有对 SearchAutoComplete 的引用,以避免在每次文本更改时都使用 findViewById:

You should also hold a reference to SearchAutoComplete to avoid findViewById on every text change:

SearchAutoComplete mSearchSrcTextView = seachView.findViewById(R.id.search_src_text);