SearchView建议-版式宽度:match_parent [英] SearchView Suggestion - Layout width: match_parent

查看:52
本文介绍了SearchView建议-版式宽度:match_parent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示使用appcompat-v7:21使用整个屏幕宽度的SearchView 建议?

How to display SearchView suggestions which use the whole screen width using appcompat-v7:21?

我在代码和菜单资源中使用android.support.v7.widget.SearchView.新的工具栏小部件具有searchViewStyle,但是我找不到一个参数来显示建议的全角(match_parent).

I use android.support.v7.widget.SearchView in code and the menu-resource. The new Toolbar widget has a searchViewStyle, but I couldn't find a parameter to display suggestions full width (match_parent).


(来源: netdna-cdn.com )


(source: netdna-cdn.com)

推荐答案

您可以这样做:(位于DropDownAnchor上)

you can do it like this: (it's on DropDownAnchor)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    int searchEditTextId = R.id.search_src_text;
    final AutoCompleteTextView searchEditText = (AutoCompleteTextView) searchView.findViewById(searchEditTextId);
    final View dropDownAnchor = searchView.findViewById(searchEditText.getDropDownAnchor());

    if (dropDownAnchor != null) {
        dropDownAnchor.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {

                // screen width
                int screenWidthPixel = ActivitySearchUni.this.getResources().getDisplayMetrics().widthPixels;
                searchEditText.setDropDownWidth(screenWidthPixel);
            }
        });
    }

    return true;
}

这篇关于SearchView建议-版式宽度:match_parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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