【问题标题】:Android searchView drop down menu screen widthAndroid searchView 下拉菜单屏幕宽度
【发布时间】:2016-02-15 05:26:07
【问题描述】:

情况:

我的 appcompat 工具栏中有一个 searchview 小部件,它允许通过 sqlitedatabase 提供自定义建议。

问题:

我无法将下拉建议列表扩展到整个屏幕的宽度。充其量,列表宽度几乎是屏幕的宽度,除了左侧和右侧的小边距/填充问题。如何使下拉列表与屏幕的宽度相同?

我尝试过的事情:

SearchView results list width

AutoCompleteText的setPadding,setRight/setLeft,setDropDownHorizo​​ntalOffset,设置布局参数,设置宽度为match_parent。

菜单/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);
            }
        });
    }

【问题讨论】:

    标签: android searchview search-suggestion


    【解决方案1】:

    你想要的宽度大小,可以通过 Wrap content 来实现。 试试这个代码: searchAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

    当我浏览这个链接时,我得到了这个答案:

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

    【讨论】:

    • 我尝试将它添加到 addOnLayoutChangeListener 之外,然后再添加到内部。与我的原始代码没有区别。
    • 好的,请在 xml 中使用 AutocompleteTextview 而不是 textview 并保持 layout_width ="fill parent"。
    【解决方案2】:

    您必须获取 SearchView 使用的 SearchAutoComplete 对象的引用,然后设置其大小并更改具有填充的背景资源,请在此处参考我的答案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2015-01-21
      • 2012-11-29
      • 2016-12-25
      相关资源
      最近更新 更多