我正在使用 AppCompat v21 库在我的应用程序中使用 Toolbar
作为操作栏,如 this post 中所述在 Android 开发者博客上。我使用ThemeOverlay.AppCompat.Dark.ActionBar
主题对操作栏进行了样式设置,以使文本变浅。但是,我想让 Action Bar SearchView
的搜索建议弹出窗口在浅色背景上显示深色文本,我一直无法实现此效果。
这是我的操作栏 Toolbar
的 XML:
<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"/>
我已经尝试扩展操作栏主题覆盖以设置 SearchView
的样式:
<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="searchViewStyle">@style/Widget.AppCompat.Light.SearchView</item>
</style>
我已将相同的元素添加到我的主题中以进行良好的衡量:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="searchViewStyle">@style/Widget.AppCompat.Light.SearchView</item>
</style>
但是 searchViewStyle
元素似乎没有任何作用。如何在此处设置 SearchView
样式?
最佳答案
根据 AppCompat v21 announcement blog post :
AppCompat 提供 Lollipop 更新后的 SearchView API,该 API 更加可定制和可样式化(让掌声排队)。我们现在使用 Lollipop 样式结构而不是旧的 searchView* 主题属性。
以下是您对 SearchView 的样式(在 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>
如 this blog post 中进一步研究.
关于android - 将工具栏用作操作栏时如何设置 SearchView 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017396/