【问题标题】:How to style the cursor color of SearchView under AppCompatAppCompat下如何设置SearchView的光标颜色
【发布时间】:2015-02-28 01:58:18
【问题描述】:

我的 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 的颜色看起来很奇怪,光标和底线显示为白色,并快速转换为强调色,如何处理?我想让光标和底线保持白色。

【问题讨论】:

标签: android android-appcompat android-styles


【解决方案1】:

经过大量实验,我终于能够通过使用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>

这是光标分别在应用新主题之前和之后的样子...

之前

之后

最后一点,您始终可以通过修改 Drawable 中的相应字段来更改新光标的宽度和颜色:

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

【讨论】:

  • 这段代码没有效果 @drawable/cursor 就够了
  • 请注意,这也是 Google 推荐的官方解决方案 - 请参阅 code.google.com/p/android/issues/detail?id=182516。谢谢!
  • 而不是&lt;item name="android:autoCompleteTextViewStyle"&gt;@style/cursorColor&lt;/item&gt;尝试使用&lt;item name="autoCompleteTextViewStyle"&gt;@style/cursorColor&lt;/item&gt;
  • android:textCursorDrawable 适用于 Api 12+。
  • 这似乎已停止为我工作。有没有其他人有这个问题?详情请见github.com/OneBusAway/onebusaway-android/issues/718
【解决方案2】:

我非常简单地通过在我的工具栏主题中添加一个 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 主题:

<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 文档:developer.android.com/guide/topics/ui/themes.html
  • @Nativ 感谢您的回复。当他们发布 AppCompat v21 时,谷歌在他们的博客文章中特别描述了如何为支持工具栏设置主题。它使用相同的主题方法。 android-developers.blogspot.com/2014/10/…。也就是说,我承认最初的问题是专门询问 SearchView,并且这个答案是针对工具栏中的 SearchView 量身定制的(虽然它可能与独立的 SearchView 一起使用,但我没有尝试过)。但是,博客文章表明“您不能将主题添加到小部件”指南并不总是正确的。
  • 感谢您的精彩解释。 Android 有时可能会令人困惑
【解决方案3】:

如果您只想更改 search-editText 的插入符号/光标的颜色,您可以使用这个:

<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>

【讨论】:

  • 不是一种有效的方法。它影响了很多 UI 元素
  • @GökselGüren 但您可以将此主题添加到特定视图。那么这个解决方案就可以了。
  • @GökselGüren 仅在您希望的时间和地点使用它。您不必实际将其设置为应用程序的主题...这只是一个示例
【解决方案4】:

我是如何解决的。

我的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 库。
  • 我正在使用 androidx
  • 对不起兄弟,这个答案只适用于 v7 支持库,我没有测试过新的 androidx 库。
  • @HeathBorders 仅供参考。
  • 不幸的是,这不适用于 AndroidX 版本的 SearchView(或者我可能做错了什么)。
【解决方案5】:

这个简单的解决方案对我有用:


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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 2023-03-08
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    相关资源
    最近更新 更多