将工具栏用作操作栏时,如何设置SearchView的样式? [英] How can I style the SearchView when using a Toolbar as an Action Bar?

查看:58
本文介绍了将工具栏用作操作栏时,如何设置SearchView的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AppCompat v21库在应用程序中使用Toolbar作为操作栏,如

I'm using a Toolbar as an Action Bar in my app using the AppCompat v21 library, as described in this post on the Android Developers Blog. I've styled the action bar using theThemeOverlay.AppCompat.Dark.ActionBar theme so that the text is light. However, I'd like to make the Action Bar SearchView's search suggestion popup display dark text on a light background, and I've been unable to achieve this effect.

这是我的操作栏Toolbar的XML:

Here's the XML for my Action Bar 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"/> 

我尝试扩展操作栏主题叠加以设置SearchView的样式:

I've tried extending the Action Bar theme overlay to style the SearchView:

<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="searchViewStyle">@style/Widget.AppCompat.Light.SearchView</item>
</style>

为了达到良好的效果,我已经在主主题中添加了相同的元素:

I've added this same element to my main theme for good measure:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/Widget.AppCompat.Light.SearchView</item>
</style>

但是searchViewStyle元素似乎没有任何作用.如何在这里设置SearchView的样式?

But the searchViewStyle elements don't seem to have any effect. How can I style the SearchView here?

推荐答案

在深入研究源代码并进行了一些试验之后,我想出了一个解决方案,并消除了一些混乱的根源.首先,需要在应用主题中设置SearchView样式:

After digging into the source code and experimenting a bit, I've come up with a solution and cleared up a couple of sources of confusion. First, the SearchView style needs to be set in the app theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>

然后我们需要在searchViewStyle中设置suggestionRowLayout:

Then we need to set the suggestionRowLayout in the searchViewStyle:

<style name="MySearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <item name="suggestionRowLayout">@layout/search_suggestion_row</item>
</style>

在我的问题中,我以为我们可以使用@style/Widget.AppCompat.Light.SearchView来获得简单的建议菜单,但事实证明并非如此.我们需要定义自己的布局.我基于AppCompat v21库中的abc_search_dropdown_item_icons_2line.xml.

In my question, I assumed that we could use @style/Widget.AppCompat.Light.SearchView to get a light suggestion menu, but it turns out that this isn't the case; we need to define our own layout. I based mine on abc_search_dropdown_item_icons_2line.xml from the AppCompat v21 library.

这篇关于将工具栏用作操作栏时,如何设置SearchView的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆