android - 如何在 searchView 和下拉列表之间添加空间(例如 height = 20dp)?

标签 android searchview

在我的 fragment 中(不在工具栏中)我有 SearchView 组件。 这里 fragment 在 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>

这里是我 fragment 中的 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)?我该怎么做?

最佳答案

请使用此方法

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
}

关于android - 如何在 searchView 和下拉列表之间添加空间(例如 height = 20dp)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790992/

相关文章:

android - 无法列出 ../node_modules/.bin/npm' 的内容。安卓发布

android - React-native + Relay.js 的稳定构建

android - ActionBarSherlock - SearchView 为空?

android - SearchView 语音识别器不转录文本

搜索过滤 ListView 的 Android IndexOutOfBoundsException 错误

安卓 : In App Purchase

Android - "firebase.test.lab"设置无法从 Firebase 分析中排除 Play 商店测试 Activity

java - 处理最终字段为空的 csv 字符串时,myString.split( ",") 返回不正确的数组条目数

android - 自定义搜索 View 中的关闭图标

android - 在简单布局中添加 SearchView