谷歌搜索类似SearchView

时间:2018-05-13 18:19:12

标签: android android-layout android-view searchview android-styles

我想设置为灰色(图标和文字),但现在显示为白色。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="end">

    <TextView
        android:id="@+id/tv_toolbar_title"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Titulo_ventana"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="end"
        android:orientation="horizontal"
        android:padding="10dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="@dimen/cardview_default_elevation">

            <android.support.v7.widget.SearchView
                android:id="@+id/mySearchview"
                android:label="@string/app_name"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:theme="@style/Base.Widget.AppCompat.SearchView">
            </android.support.v7.widget.SearchView>

        </android.support.v7.widget.CardView>


    </LinearLayout>



</LinearLayout>

我希望在工具栏和灰色文字颜色中找到类似此搜索视图的内容:

左右菜单图标是可选的。 搜索视图默认是可折叠的,我不介意是否应该更改此功能。

enter image description here

4 个答案:

答案 0 :(得分:1)

我有一个解决方案,它在我的应用程序中正常工作。你可以试试这个。

EditText txtSearch = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
txtSearch.setHint(getResources().getString(R.string.search));
txtSearch.setHintTextColor(Color.GRAY);
txtSearch.setTextColor(Color.GRAY);

答案 1 :(得分:1)

您想要实现的搜索视图不是SearchView。这是一个自定义组件。这是可以使用Hierarchy Viewer工具获得的视图树:

enter image description here

这是OverlaySearchPlateContainer

enter image description here

从包名称可以看出,它随播放服务一起提供,因此源代码不可用。

然而,我们可以将该树与SearchView的树进行比较。这是在使用普通SearchView时可以获得的树:

enter image description here

因此,您可以看到OverlaySearchPlateContainer SearchView的后代,因此您想要实现的UI无法仅使用样式/主题来完成。

我建议不要重新发明轮子并使用第三方库,例如SearchBar-SearchView,其中SearchView小部件如下所示:

enter image description here

答案 2 :(得分:0)

要更改文字颜色,请将其放在styles.xml

<style name="MySearchView" parent="Base.Widget.AppCompat.SearchView">
    <item name="android:editTextColor">@color/text_color</item>
    <item name="android:textColorHint">@color/hint_color</item>
</style>

替换@color/text_color@color/hint_color以满足您的需求。

然后更改XML布局中的SearchView主题属性:

<android.support.v7.widget.SearchView
    android:id="@+id/mySearchview"
    android:label="@string/app_name"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:theme="@style/MySearchView">
</android.support.v7.widget.SearchView>

答案 3 :(得分:0)

  

您可以设置搜索视图的背景

<android.support.v7.widget.SearchView
            android:id="@+id/searchViewContact"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            app:searchIcon="@mipmap/icon_search"
            app:closeIcon="@mipmap/icon_close"
            android:background="@drawable/search_view_background"
            android:layout_margin="5dp">
</android.support.v7.widget.SearchView>
  

search_view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#dcdee1"/>
    <stroke
        android:color="#d7d3d3"
        android:width="1dp"/>
    <corners
        android:radius="5dp"/>
</shape>