如何在AppCompat下设置SearchView的光标颜色?

46

我的SearchView是android.support.v7.widget.SearchView,AppTheme如下所示。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/color_accent</item>
</style>

<style name="AppTheme" parent="AppBaseTheme">
</style>

搜索视图(SearchView)的颜色看起来很奇怪,它的光标和底部线条显示为白色并迅速转换为强调色,如何处理?我想让光标和底部线保持白色。


这个答案对我很有帮助。 - Sufian
5个回答

75

进行了很多实验后,我最终能够使用autoCompleteTextViewStyle属性和自定义光标Drawable来改变光标颜色。修改您在示例中提供的代码,您可以这样做。首先,您将如下所示将上述属性添加到您的主题中:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/color_accent</item>
    <item name="autoCompleteTextViewStyle">@style/cursorColor</item>
</style>

接下来,您需要创建 "cursorColor" 的样式,该样式将引用您将要创建的光标 Drawable(下一步):

<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
        <item name="android:textCursorDrawable">@drawable/cursor</item>
</style>

最后要做的是创建光标可绘制对象(cursor.xml),它将用作替换光标:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#FFFFFF" />
    <size android:width="1dp" />
</shape>

以下是应用新主题前后光标的样子:

应用前

enter image description here

应用后

enter image description here

最后需要注意的是,你可以通过修改Drawable中相应的字段来随时更改新光标的宽度和颜色:

<!-- Change this to increase the width -->
<size android:width="1dp"/>
<!-- Change this to change the color -->
<solid android:color="#FFFFFF" />

6
这段代码没有影响: <item name="android:textCursorDrawable">@drawable/cursor</item> 已足够。 - Rahul
1
请注意,这是谷歌推荐的官方解决方案-请参见https://code.google.com/p/android/issues/detail?id=182516。谢谢! - Sean Barbeau
8
建议使用<item name="autoCompleteTextViewStyle">@style/cursorColor</item>代替<item name="android:autoCompleteTextViewStyle">@style/cursorColor</item> - user3473445
android:textCursorDrawable 是针对 Api 12+ 的。 - wangqi060934
1
这个似乎对我来说已经停止工作了。还有其他人遇到过这个问题吗?请参见https://github.com/OneBusAway/onebusaway-android/issues/718获取详细信息。 - Sean Barbeau
显示剩余5条评论

30

我通过在我的工具栏主题中专门添加了一个colorAccent,解决了这个问题,该颜色与我应用程序其余部分的colorAccent不同。

例如,对于应用程序基础,我有一种特定的强调色彩,我希望在整个应用程序中都有:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/color_accent</item>
</style>

然后对于我的工具栏,其中包含搜索视图,我已经设置了一个安卓主题:

<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"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/toolBarBackgroundColor"
    android:theme="@style/ToolbarStyle"
    app:popupTheme="@style/ThemeOverlay.AppCompat" />

在那个工具栏主题中,我使用另一种颜色强调来特别标记工具栏中需要强调的元素(在我的情况下只有光标):

<style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    ...
    <item name="colorAccent">@color/cursorAccent</item>
</style>

这个解决方案不好,因为你只能将主题添加到活动或应用程序,而不能将其添加到小部件。摘自Android文档:https://developer.android.com/guide/topics/ui/themes.html - Nativ
@Nativ 谢谢您的回复。当他们发布AppCompat v21时,Google在他们的博客文章中特别描述了如何对支持工具栏进行主题设置。它使用了相同的主题方法。http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html。话虽如此,我承认最初的问题是关于SearchView的,而这个答案是针对Toolbar内的SearchView的(尽管它可能适用于独立的SearchView,但我没有尝试过)。然而,该博客文章建议,“您不能向小部件添加主题”准则并不总是正确的。 - tabjsina
非常感谢您的出色解释。有时候安卓确实会让人感到困惑。 - Nativ

24

如果你只想改变搜索编辑框中插入符/光标的颜色,你可以使用以下代码:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarTheme">@style/AppTheme.ActionBarTheme</item>
.... 

<style name="AppTheme.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlActivated">#FFffffff</item>
</style>

9
不是一种高效的方式。它会影响许多用户界面元素。 - Göksel Güren
1
@GökselGüren 但你可以将这个主题添加到特定的视图中。这个解决方案就很好了。 - PrzemekTom
@GökselGüren 只需在您希望的时间和地点使用它。您不必将其实际设置为应用程序的主题...这只是一个例子。 - android developer

11

我是如何解决的。

我的SearchView代码:

<android.support.v7.widget.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/WallSearchView"
                app:queryHint="@string/action_search"
                app:searchIcon="@drawable/ic_search" />

我的WallSearchView样式是:

<style name="WallSearchView" parent="Widget.AppCompat.SearchView">
        ...
        <item name="iconifiedByDefault">false</item>
        <item name="colorControlActivated">#FFffffff</item>
    </style>

注意:此内容仅适用于 v21 及以上版本。


您正在使用v7或androidx库。 - Anand Kumar
我正在使用androidx。 - Heath Borders
抱歉,兄弟,这个答案只适用于v7支持库,我还没有测试过新的androidx库。 - Anand Kumar
@HeathBorders FYI. - Anand Kumar
不幸的是,这对于AndroidX版本的SearchView无效(或者我可能做错了什么)。 - Bip901

8
这个简单的解决方案对我有用:
1 - 在Styles.xml文件中定义一个样式
<style name="WhiteCursorSearchView" parent="Widget.AppCompat.SearchView">
    <item name="colorControlActivated">#FFFFFF</item>
</style>

2- 将该样式应用于您的SearchView

<android.support.v7.widget.SearchView
    android:id="@+id/search"
    android:theme="@style/WhiteCursorSearchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:iconifiedByDefault="false" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接