【问题标题】:SearchView Suggestion - Layout width: match_parentSearchView 建议 - 布局宽度:match_parent
【发布时间】:2015-03-12 20:25:10
【问题描述】:

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

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


(来源:netdna-cdn.com

【问题讨论】:

标签: android android-layout android-appcompat searchview


【解决方案1】:

你可以这样做:(它在 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;
}

【讨论】:

    猜你喜欢
    • 2022-08-14
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    相关资源
    最近更新 更多