搜索查看结果列表宽度 [英] SearchView results list width
问题描述
有没有一种方法,使搜索查看的结果列表占据整个屏幕宽度?我已经使用自定义布局搜索查看试过,但是这并不会改变搜索结果列表的宽度。 见截图:
Is there a way to make SearchView's results list occupy whole screen width? I've tried using custom layout for SearchView, but this doesn't change the search results list's width. See screenshot:
推荐答案
是的,这是可能的。要做到这一点,我们应该设置宽度为整个 DropDownView
,不仅为项目。我们可以通过调用 setDropDownWidth
AutoCompleteTextView
的设置。但有一个问题。 DropDownView
的宽度和横向偏移其边界改变时计算里面搜索查看
。为了得到一个解决办法,我们可以添加到搜索查看
我们自己 OnLayoutChangeListener
在这里我们计算并设定<$ C $的高度C> DropDownView 。下面code完全正与 AppCompat
:
Yes, it is possible. To achieve this we should set width of whole DropDownView
, not only for items. We can set it by calling setDropDownWidth
of AutoCompleteTextView
. But there is one problem. DropDownView
's width and horizontal offset are calculated inside SearchView
each time its bounds is changed. To get a workaround we can add to SearchView
our own OnLayoutChangeListener
where we calculate and set the height of DropDownView
. The following code is fully working with AppCompat
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// inflate our menu
getMenuInflater().inflate(R.menu.search_menu, menu);
// find MenuItem and get SearchView from it
MenuItem searchMenuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
// id of AutoCompleteTextView
int searchEditTextId = R.id.search_src_text; // for AppCompat
// get AutoCompleteTextView from SearchView
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) {
// calculate width of DropdownView
int point[] = new int[2];
dropDownAnchor.getLocationOnScreen(point);
// x coordinate of DropDownView
int dropDownPadding = point[0] + searchEditText.getDropDownHorizontalOffset();
Rect screenSize = new Rect();
getWindowManager().getDefaultDisplay().getRectSize(screenSize);
// screen width
int screenWidth = screenSize.width();
// set DropDownView width
searchEditText.setDropDownWidth(screenWidth - dropDownPadding * 2);
}
});
}
return super.onCreateOptionsMenu(menu);
}
如果您使用全息只是改变了行 INT searchEditTextId = R.id.search_src_text;
到下面的一个:
If you use Holo just change the line int searchEditTextId = R.id.search_src_text;
to the below one:
int searchEditTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
这篇关于搜索查看结果列表宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!