Android searchview建议填充权限

时间:2016-12-12 19:14:20

标签: android searchview

我的搜索视图存在问题。我试图将搜索建议全宽,但建议有正确的填充。

my app searchview

< - language:lang-java - >

<input type = "text" id="one" class="blurrable">
<br>
<input type = "text" id="two" class="blurrable">

$(".blurrable").blur(function () {
    alert("Hello");
});

我的菜单xml:

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;
}

我尝试了这些Stack Overflow答案:

solution 1

solution 2

solution 3

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

试试这个

searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);

而不是

searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

答案 1 :(得分:0)

这是我的解决方案, 只设置SearchView.SearchAutoComplete视图DropDownWidth以匹配屏幕的父级或宽度不会起作用,因为它受到所有方面都有填充的背景资源的限制。

所以我明白为了让它发挥作用,我们必须改变背景。

解决方案: (每次视图布局尺寸更改时都必须这样做):

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
            mSearchSrcTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels);
            mSearchSrcTextView.setDropDownBackgroundResource(R.color.colorGray);

            modelManager.getLocationsSearchResults(query);
            return true;
        }
});

Result

答案 2 :(得分:0)

这仅对我有用:

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

您必须设置具有所需宽度的锚视图。其他下拉列表将不会变得比输入字段宽。我只是使用searchView的父项。

如@ stav-bodik所述:需要在每个onQueryTextChange上调用它。如果仅执行一次,则似乎不起作用。 (不需要更改背景)

您还应该保留对SearchAutoComplete的引用,以避免每次文本更改时都出现findViewById:

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