如何在searchView和下拉列表之间添加空格(例如,高度= 20dp)?

时间:2017-10-17 13:10:07

标签: android searchview

在我的片段(不在ToolBar中)中,我有SearchView组件。 这里是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="match_parent">

    <android.support.v7.widget.SearchView
        android:id="@+id/searchView"
        style="@style/Widget.AppCompat.SearchView.ActionBar"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:background="@drawable/search_view_bg"
        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:layout_marginEnd="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        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>

我的片段中的java代码:

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

结果如下:

SearchView_and_dropdown

但是我需要在searchView和下拉列表之间添加空格(高度= 20dp)?我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

请使用此方法

private void setDropdownWidthHeight() {
 SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete) 
 searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
 mSearchSrcTextView.setDropDownVerticalOffset(100);  //height in pixels
}