SearchView Suggestion - Layout width: match_parent

谁都会走 提交于 2019-12-07 03:56:11

问题


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

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).


(source: netdna-cdn.com)


回答1:


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


来源:https://stackoverflow.com/questions/27946569/searchview-suggestion-layout-width-match-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!