给EditText添加下划线

在安卓高版本,默认是有下划线的,其默认下划线的颜色是由其主题颜色来控制的!
控制如下:

 <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/colorPrimaryDark</item>**

所以,只需要修改colorAccent的颜色,其下划线的颜色既可以修改!

在低版本和高版本中,同样是可以去添加下划线的!方法有二:
方法一:

//此时必须要设置其背景为空
<EditText
        android:background="@null"
        android:drawableBottom="@drawable/line"
        android:hint="请输入您的手机号码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  //资源名称为 drawable/line
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorBlue" />
    <size
        android:height="1dp"
        android:width="1000dp" />
</shape>

方法二:通过自定义editText

public class UnderLineEditText extends EditText {
    private Paint paint;

    public UnderLineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        //设置画笔的属性
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        //设置画笔颜色为红色
        paint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**canvas画直线,从左下角到右下角,this.getHeight()-2是获得父edittext的高度,但是必须要-2这样才能保证
         * 画的横线在edittext上面,和原来的下划线的重合
         */
        canvas.drawLine(0, this.getHeight()-2, this.getWidth()-2, this.getHeight()-2, paint);
    }
}

这里有几点需要注意:
其一:也可以继承android.support.v7.widget.AppCompatEditText,但是有时会出现获取不到焦点的现状
其二:下划线的的位置确定

**方法三:**使用layer-list来显示边框线,drawable新建资源文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 
    <!--底层使用蓝色填充色-->
    <item>
        <shape>
            <solid android:color="#02a0ef"/>
        </shape>
    </item>
 
    <!--上面一层距离底层的顶部1dp,类似marginTop,填充色为白色,这样就形成了一个带有蓝色顶部边线的白色背景的图-->
    <item android:top="1dp">
        <shape>
            <solid android:color="#fff"/>
        </shape>
    </item>
</layer-li

以上!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值