自定义Edt 光标和下划线

EditText 有一个属性:android:textCursorDrawable ,它就是用来设置光标样式的。

我们可以在drawable资源文件夹下新建一个cursor_color.xml文件,内容如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="2dp" />
    <solid android:color="@android:color/holo_blue_light" />
</shape>
然后设置
<EditText
 android:textCursorDrawable="@drawable/cursor_color"

接下来是下划线颜色,先定义一个Style

<style name="MyEditText" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@android:color/darker_gray</item>
        <item name="colorControlActivated">@android:color/holo_orange_dark</item>
    </style>
colorControlNormal  表示控件默认的颜色, colorControlActivated  表示控件被激活时的颜色,这样,我们就可以分别设置EditText不被选中和选中时的颜色了。

然后在要改样式的Edt里添加一条  android:theme="@style/MyEditText" 就可以了

但是这样子选中状态的下划线粗线还是和未选中不一样,如果要控制粗线,可以自己写一个drawable文件,内容用layer-list和selector 来做,如下  

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#cdcdcd"/>
                </shape>
            </item>
            <item android:bottom="2dp">
                <shape>
                    <solid android:color="@color/white"/>
                </shape>
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#cdcdcd"/>
                </shape>
            </item>
            <item android:bottom="1dp">
                <shape>
                    <solid android:color="@color/white"/>
                </shape>
            </item>
        </layer-list>
    </item>
</selector> 

这样用layer-list ,就可以控制下划线的颜色和粗细,具体layer-list 的用法,请百度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值