如何在 AppCompat 下设置 SearchView 的光标颜色样式 [英] How to style the cursor color of SearchView under AppCompat

查看:40
本文介绍了如何在 AppCompat 下设置 SearchView 的光标颜色样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My SearchView is android.support.v7.widget.SearchView and AppTheme is showed below.

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

The color of SearchView seems strange, its cursor and bottom line showed white and trans to the accent color quickly, how to deal with it? I want to make the cursor and bottom line stay white.

推荐答案

经过大量实验,我终于能够通过使用 autoCompleteTextViewStyle 属性以及自定义光标<代码>可绘制.修改您在示例中提供的代码,您将执行如下操作.首先,您将上述属性添加到您的主题中,如下所示:

After alot of experimentation, I was finally able to change the cursor color by using the autoCompleteTextViewStyle attribute, along with a custom cursor Drawable. Modifying the code you provided in the example, you would do something like the following. First, you add the aforementioned attribute to your main theme as follows:

<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(下一步):

You then create the style for "cursorColor" which will reference the cursor Drawable that you will create (next step):

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

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

The final thing to do is to to create the cursor drawable (cursor.xml) that will be used as the replacement cursor:

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

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

This is what the cursor looks like before and after applying the new theme, respectively...

之前

之后

最后一点,您可以随时通过修改 Drawable 中的适当字段来更改新光标的宽度和颜色:

As a final note, you can always alter the width and color of the new cursor by modifying appropriate fields in your Drawable:

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

这篇关于如何在 AppCompat 下设置 SearchView 的光标颜色样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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