Android searchView drop down menu screen width

ⅰ亾dé卋堺 提交于 2019-12-31 04:26:17

问题


Situation:

I have a searchview widget in my appcompat toolbar that allows custom suggestions via sqlitedatabase.

Problem:

I am having trouble expanding the drop down suggestions list to be the full width of the screen. At best, the list width is almost the width of the screen except for small margin/padding problems on the left and right side. How do I make the drop down list have the same width as the screen?

Things I've tried:

SearchView results list width

AutoCompleteText's setPadding, setRight/setLeft, setDropDownHorizontalOffset, setting layout params, and setting width to be match_parent.

menu/search.xml

<item
    android:id="@+id/search_restaurant"
    android:title="Search"
    android:icon="@drawable/search"
    android:orderInCategory="2"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"/>

res/search_view_suggestions_list_item

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

activity/Activity.java

    MenuItem searchItem = menu.findItem(R.id.search_restaurant);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint("Search Restaurant");
    searchView.setOnQueryTextListener(this);
    searchView.setOnSuggestionListener(this);
    final AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) searchView
            .findViewById(R.id.search_src_text);
    searchAutoCompleteTextView.setThreshold(1);
    searchAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);

    // Tried to change margins/padding here
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
            searchAutoCompleteTextView.getLayoutParams();
    params.setMargins(0, 0, 0, 0);
    searchAutoCompleteTextView.setLayoutParams(params);
    searchAutoCompleteTextView.setPadding(0, 0, 0, 0);

    searchAutoCompleteTextView.setDropDownHorizontalOffset(-100);
    searchAutoCompleteTextView.setPadding(0,0,0,0);
    searchAutoCompleteTextView.setLeft(0);

    final View dropDownSuggestions = searchView.findViewById(searchAutoCompleteTextView
            .getDropDownAnchor());
    if (dropDownSuggestions != null) {
        dropDownSuggestions.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) {
                int point[] = new int[2];

                dropDownSuggestions.getLocationOnScreen(point);
                int dropDownPadding = point[0] + searchAutoCompleteTextView
                        .getDropDownHorizontalOffset();

                Rect screenSize = new Rect();
                getWindowManager().getDefaultDisplay().getRectSize(screenSize);
                int screenWidth = screenSize.width();
                searchAutoCompleteTextView.setDropDownWidth(screenWidth * 2);
            }
        });
    }

回答1:


The size of the width you want, can be achieve by using Wrap content . Try this code: searchAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

I came to this answer when i went through this link:

https://www.youtube.com/watch?v=408pMAQFnvs




回答2:


You must get reference to SearchAutoComplete object which SearchView using , then set its size and change the background resource which has padding's, please refer to my answer here :

https://stackoverflow.com/a/43481639/5255624



来源:https://stackoverflow.com/questions/35402030/android-searchview-drop-down-menu-screen-width

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