Google 搜索相似 SearchView
Google search alike SearchView
我想设置为灰色(图标和文本),但现在显示为白色。
<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>
我想在工具栏和灰色文本颜色中找到类似于此搜索视图的内容:
左右菜单图标可选。
默认情况下搜索视图是可折叠的,我不介意是否应该更改此功能。
要更改文本颜色,请将其放入 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>
我有一个解决方案,它在我的应用程序中运行良好。你可以试试这个。
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);
You can set the background of search view
<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>
您要实现的搜索视图实际上不是SearchView
。这是一个自定义组件。这是可以使用 Hierarchy Viewer 工具获得的视图树:
这是 OverlaySearchPlateContainer
:
从包名可以看出,它是随Play服务一起提供的,因此没有源代码。
不过,我们可以将那棵树与 SearchView
的树进行比较。这是使用普通SearchView
时可以获得的树:
因此,您可以看到,OverlaySearchPlateContainer
不是 SearchView
的后代,因此您想要实现的 UI不能仅使用 styles/themes.
来完成
我建议不要重新发明轮子并使用第 3 方库,例如 SearchBar-SearchView,其中 SearchView
小部件如下所示:
我想设置为灰色(图标和文本),但现在显示为白色。
<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>
我想在工具栏和灰色文本颜色中找到类似于此搜索视图的内容:
左右菜单图标可选。 默认情况下搜索视图是可折叠的,我不介意是否应该更改此功能。
要更改文本颜色,请将其放入 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>
我有一个解决方案,它在我的应用程序中运行良好。你可以试试这个。
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);
You can set the background of search view
<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>
您要实现的搜索视图实际上不是SearchView
。这是一个自定义组件。这是可以使用 Hierarchy Viewer 工具获得的视图树:
这是 OverlaySearchPlateContainer
:
从包名可以看出,它是随Play服务一起提供的,因此没有源代码。
不过,我们可以将那棵树与 SearchView
的树进行比较。这是使用普通SearchView
时可以获得的树:
因此,您可以看到,OverlaySearchPlateContainer
不是 SearchView
的后代,因此您想要实现的 UI不能仅使用 styles/themes.
我建议不要重新发明轮子并使用第 3 方库,例如 SearchBar-SearchView,其中 SearchView
小部件如下所示: