自定义 editText下划线

看了下网上代码:

我想要的效果如下图

自定义 editText下划线

下划线和文字有15dp的间距   eeeeee的颜色

 

 

上代码,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:bottom="0dp"
            android:left="-1dp"
            android:right="-1dp"
            android:top="-1dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke      android:color="@color/color_eeeeee"
                         android:width="1dp" />
            <padding android:bottom="15dp" />

        </shape>
    </item>

</layer-list>

 有几个点说一下   item中的left,bottom,right,top  其实是相对于Editext边缘的距离  这本身实个图层,要做的如果你都设置成0

那么 就会变成4边都有边框包裹,这里设置-1dp 是抹去不想显示的边框,因为只需要下划线  ,所以上下左全部设置成-1dp,

shape中的 stroke  中  2个属性 ,一个是color  表示边框的颜色,width表示边框的宽度。我这里设置1DP

需求是文字与下划线间隔15DP  ,那么就在<padding 中设置bottom 为15dp。

再写个selector  就可以用了

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false" android:drawable="@drawable/et_underline_selected"/>
    <item android:state_focused="true" android:drawable="@drawable/et_underline_selected"/>
</selector>

 因为选中非选重底部下划线样式一样,所以我就用了一个文件了

 

然后把这个样式设置成edittext的默认样式,以后就不用每次都调用这个了

具体操作如下

现在androidManifest中找到你系统的样式

自定义 editText下划线

自定义 editText下划线