Android 搜索视图建议填充正确 [英] Android searchview suggestions padding right

查看:55
本文介绍了Android 搜索视图建议填充正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的搜索视图有问题.我正在尝试使搜索建议全宽,但建议有正确的填充.

I have a problem with my searchview. I'm trying to make search suggestions full width but the suggestions have a right padding.

<-- 语言:lang-java -->

<-- language: lang-java -->

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    final MenuItem item=menu.findItem(R.id.procura);

    MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            tbs.setVisibility(View.GONE);
            vwpgr.setVisibility(View.GONE);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            tbs.setVisibility(View.VISIBLE);
            vwpgr.setVisibility(View.VISIBLE);
            return true;
        }
    });

    searchView=(SearchView) MenuItemCompat.getActionView(item);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint(getResources().getString(R.string.app_hint));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Intent search=new Intent(MainActivity.this,searchableactivity.class);
            search.putExtra("searchquery",query);
            startActivity(search);
            searchView.clearFocus();
            item.collapseActionView();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    //this remove left padding
    searchTextView.setDropDownAnchor(R.id.appbar);
    // this is not working
    searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    //and i tryed this too: searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, R.drawable.search_cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {
    }

    return true;
}

我的菜单 xml:

<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=".MainActivity">
<item
    android:id="@+id/procura"
    android:title="@string/app_hint"
    android:icon="@mipmap/ic_search_black_24dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

我尝试了这些 Stack Overflow 答案:

And I tried these Stack Overflow answers:

解决方案 1

解决方案 2

解决方案 3

我该如何解决这个问题?

How can I solve this?

推荐答案

只有这个对我有用:

@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);

这篇关于Android 搜索视图建议填充正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆