Android修改光标颜色

2019-10-25  本文已影响0人  dlihasa

前言:

EditText中光标默认会和当前App的主题样式(例如下面MyAppTheme)中设置的保持一致:

<application
        ...
        android:theme="@style/MyAppTheme"
        ...>

style.xml文件中AppTheme样式如下:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

默认会和colorPrimary一样。但是有时我们可能不单独设置colorPrimary或者光标颜色有特别的要求

修改光标样式

一、准备一个光标需要的drawable,可以是图片,可以使自己写的.xml图(其他网友用的说设置@null,我试了不好用

这里我们自己写一个.xml文件,还可以设置想要用的图片

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

二、将drawable设置上去,两种方式:

(1)局部修改:在Layout文件的EditText控件标签里加入:

android:textCursorDrawable="@drawable/my_cursor"

(2)全局修改:在App的主题样式中加入:

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        。。。
        <item name="android:textCursorDrawable">@drawable/my_cursor</item>
        。。。
</style>

完成。

上一篇下一篇

猜你喜欢

热点阅读