Android EditText设置下划线线段样式

在这里插入图片描述
偶然发现这种ExitText的下划线也挺好看,反编译别人的res文件发现也没有用Background ,也不知道怎么做出来的,不过条条道路通罗马,使用layer-list也可以做出这种效果

在drawable中新建et_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#ffffff"/>
            <stroke android:color="#a5a5a5" android:width="1dp"/>
        </shape>
    </item>
    <item android:bottom="6dp">
        <shape>
            <solid android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

再新建et_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#ffffff"/>
            <stroke android:color="#0099cc" android:width="1dp"/>
        </shape>
    </item>
	<!--在底部视图的底部向上6dp-->
    <item android:bottom="6dp">
        <shape>
            <solid android:color="#ffffff"/>
        </shape>
    </item>

</layer-list>

再新建et_selector.xml

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

最后在自己的EditText中引用为背景即可

    <EditText
        android:id="@+id/wakeup_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingBottom="3dp"
        android:hint="2000"
        android:background="@drawable/et_selector"
        android:inputType="number"
        />

shape默认是长方形的,
layer-list中底层视图是一个

<shape>
	<solid android:color = "#ffffff"/>
	<stroke android:color = "#a5a5a5"
		android:width = "1dp"/>
</shape>

solid内部填充色是白色,stroke 边框色是灰色的长方形。

layer-list中上层视图是一个没有边框全白的长方形。
layer-list的特点是上层视图会盖住底层视图
只要将全白的长方形盖住有边框的长方形上半部分就可以得到线段样式的EditText。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值