如何使用 appcompat-v7:21
显示使用整个屏幕宽度的 SearchView 建议?
我在代码和菜单资源中使用 android.support.v7.widget.SearchView。新的工具栏小部件有一个 searchViewStyle
,但我找不到一个参数来全宽显示建议 (match_parent)。
(来源:netdna-cdn.com)
最佳答案
你可以这样做:(它在 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;
}
关于android - SearchView 建议 - 布局宽度 : match_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27946569/