如何设置搜索视图下拉列表的宽度以匹配android中搜索文本区域的宽度

时间:2016-06-01 11:00:45

标签: android dropdown searchview

我想用advice-list实现searchview。 通常,建议列表下拉宽度与搜索视图文本区域相同。但是当我在AndroidManifest.xml中设置以下属性时 android:theme =“@ android:style / Theme.Black.NoTitleBar.Fullscreen”到应用程序标签,我看到建议列表宽度看起来比搜索视图文本区域大。 我尝试使用autoCompleteTextView.setDropdownWidth(int)将dropdownwidth设置为某​​个值,但是没有用。

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以这样做,在这里我需要用下拉列表填充屏幕宽度:

@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();


final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

    // get AutoCompleteTextView from SearchView
    final AutoCompleteTextView searchEditText = (AutoCompleteTextView) searchView.findViewById(searchEditTextId);
    final View dropDownAnchor = findViewById(searchTextView.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] + searchTextView.getDropDownHorizontalOffset();

                Rect screenSize = new Rect();
                getWindowManager().getDefaultDisplay().getRectSize(screenSize);
                // screen width
                int screenWidth = screenSize.width();

                // set DropDownView width
                searchTextView.setDropDownWidth(screenWidth - dropDownPadding * 2);
            }
        });
    }

    return super.onCreateOptionsMenu(menu);
}

原始答案: https://stackoverflow.com/a/26344053/1959110