【问题标题】:SearchView in fragment (non in toolbar) can't remove underline片段中的 SearchView(不在工具栏中)无法删除下划线
【发布时间】:2017-10-16 16:56:19
【问题描述】:

在我的片段中,我使用 searchView(不在工具栏中)。 我的布局 sn-p:

<?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"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        app:queryHint="Some hint"
        android:layout_marginStart="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:searchHintIcon="@null" />
</android.support.constraint.ConstraintLayout>

在我的片段代码中:

        searchView = view.findViewById(R.id.searchView);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setSuggestionsAdapter(new SearchCatalogSuggestionsAdapter(getActivity()));
searchView.setBackgroundColor(Color.parseColor("#FFFFFF"));

如你所见,我试试

searchView.setBackgroundColor(Color.parseColor("#FFFFFF"));

但下划线没有帮助。 这里截图:

【问题讨论】:

  • 尝试将searchview 的背景设置为null(适用于AutoCompleteTextView)。
  • @LalitSinghFauzdar SearchView 实际上是一个 LinearLayout 并且背景设置在其中的 EditText 上,所以这不起作用。
  • android:background="@null" - 没有帮助

标签: android android-layout searchview


【解决方案1】:

AppCompat 为 SearchView 提供两种样式:

  • 默认带下划线
  • 在操作栏中使用,不带下划线

你可以这样改变样式:

<android.support.v7.widget.SearchView
    style="@style/Widget.AppCompat.SearchView.ActionBar"/>

样式如下:

<style name="Widget.AppCompat.SearchView.ActionBar" parent="Base.Widget.AppCompat.SearchView.ActionBar"/>

<style name="Base.Widget.AppCompat.SearchView.ActionBar">
    <item name="queryBackground">@null</item>
    <item name="submitBackground">@null</item>
    <item name="searchHintIcon">@null</item>
    <item name="defaultQueryHint">@string/abc_search_hint</item>
</style>

当然,你可以直接使用属性。

【讨论】:

    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多