使用工具栏作为操作栏时,如何设置SearchView的样式?

Ben*_*Ben 25 android android-actionbar searchview android-5.0-lollipop android-toolbar

我正在使用ToolbarAppCompat v21库在我的应用程序中使用一个Action Bar,如Android开发者博客上的这篇文章所述.我使用ThemeOverlay.AppCompat.Dark.ActionBar主题设置了操作栏的样式,以便文本很轻.但是,我想让Action Bar SearchView的搜索建议弹出窗口在浅色背景上显示深色文字,我一直无法达到这个效果.

这是我的Action Bar的XML Toolbar:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar_actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/action_bar_elevation"
    app:theme="@style/ActionBarThemeOverlay"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 
Run Code Online (Sandbox Code Playgroud)

我尝试将Action Bar主题叠加扩展为样式SearchView:

<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="searchViewStyle">@style/Widget.AppCompat.Light.SearchView</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我已经将这个相同的元素添加到我的主题中以获得良好的衡量标准:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/Widget.AppCompat.Light.SearchView</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但这些searchViewStyle元素似乎没有任何影响.我怎么样SearchView这里的风格?

ian*_*ake 44

根据AppCompat v21公告博客文章:

AppCompat提供了Lollipop更新的SearchView API,它更具可定制性和可定制性(排队掌声).我们现在使用Lollipop样式结构而不是旧的searchView*主题属性.

以下是SearchView(in values/themes.xml)的样式:

<style name="Theme.MyTheme" parent="Theme.AppCompat">
   <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
   <!-- Background for the search query section (e.g. EditText) -->
   <item name="queryBackground">...</item>
   <!-- Background for the actions section (e.g. voice, submit) -->
   <item name="submitBackground">...</item>
   <!-- Close button icon -->
   <item name="closeIcon">...</item>
   <!-- Search button icon -->
   <item name="searchIcon">...</item>
   <!-- Go/commit button icon -->
   <item name="goIcon">...</item>
   <!-- Voice search button icon -->
   <item name="voiceIcon">...</item>
   <!-- Commit icon shown in the query suggestion row -->
   <item name="commitIcon">...</item>
   <!-- Layout for query suggestion rows -->
   <item name="suggestionRowLayout">...</item>
</style>
Run Code Online (Sandbox Code Playgroud)

正如在这篇博文中进一步研究的那样.

  • 好的,我需要在我的主题中使用`<item name ="searchViewStyle">`而不是`<item name ="android:searchViewStyle">`,它现在可以使用了. (3认同)

Ben*_*Ben 10

在深入研究源代码并进行了一些实验之后,我提出了一个解决方案,并澄清了一些混乱的来源.首先,SearchView需要在app主题中设置样式:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,我们需要设置suggestionRowLayoutsearchViewStyle:

<style name="MySearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <item name="suggestionRowLayout">@layout/search_suggestion_row</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在我的问题中,我认为我们可以@style/Widget.AppCompat.Light.SearchView用来获得一个轻量级的建议菜单,但结果却并非如此; 我们需要定义自己的布局.我是abc_search_dropdown_item_icons_2line.xml从AppCompat v21库开始的.

  • 别担心朋友。我能够解决它。只需在工具栏布局中设置应用程序主题即可。谢谢!这篇文章对我有帮助。 (2认同)