SearchView(不在toolBar中)如何显示全长屏幕的下拉列表

时间:2017-10-17 10:47:53

标签: android searchview

在我的片段(不在ToolBar中)中,我有SearchView组件。 片段的布局:

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="0dp"
        android:layout_height="30dp"
        app:defaultQueryHint="@null"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:searchHintIcon="@null" />


</android.support.constraint.ConstraintLayout>

这是我的建议在xml文件中的项目布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/standard_min_height_container">

    <ImageView
        android:id="@+id/catalogImageView"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:src="@drawable/suggestion_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginStart="@dimen/standard_margin" />

    <TextView
        android:id="@+id/catalogNameTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Test text View Test text "
        android:textColor="@android:color/black"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="@+id/catalogImageView"
        app:layout_constraintLeft_toRightOf="@+id/catalogImageView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/catalogImageView" />

    <View
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="@color/divider_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@+id/catalogImageView"
        app:layout_constraintRight_toRightOf="@+id/catalogNameTextView" />


</android.support.constraint.ConstraintLayout>

这里是xml文件中片段中的java代码:

searchV = view.findViewById(R.id.searchView);
SearchManager searchMng = getSystemService(Context.SEARCH_SERVICE);
searchV.setSearchableInfo(searchMng.getSearchableInfo(getComponentName()));
searchV.setSuggestionsAdapter(new SearchCatalogSuggestionsAdapter(ctx);

结果如下:

SearchView drop down

但我需要消除差距。我希望下拉以显示完整的长度。

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

首先尝试计算屏幕尺寸

//make one global variable for popup width
int suggestionPopUpWidth;

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
suggestionPopUpWidth = size.x;


//Use this method for setting width
private void setDropdownWidthHeight() {
SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchSrcTextView.setDropDownWidth((int) (suggestionPopUpWidth * 0.9));
}

在设置建议适配器

后调用此方法setDropdownWidthHeight