Android searchView下拉菜单屏幕宽度 [英] Android searchView drop down menu screen width

查看:306
本文介绍了Android searchView下拉菜单屏幕宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我的appcompat工具栏中有一个searchview小部件,可通过sqlitedatabase自定义建议.

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

问题:

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

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?

我尝试过的事情:

SearchView结果列表宽度

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

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

menu/search.xml

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

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

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);
            }
        });
    }

推荐答案

所需的宽度大小可以通过使用Wrap content来实现. 试试下面的代码: searchAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

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

这篇关于Android searchView下拉菜单屏幕宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆