Android 实现 EditText 添加下划线 Android 中的 EditText 控件在高版本中具有默认的下划线,而在低版本中需要手动添加。下面将详细介绍 Android 中实现 EditText 添加下划线的两种方法。 方法一:使用 XML 属性 在高版本的 Android 中,默认的下划线颜色是由主题颜色控制的,可以通过修改 `colorAccent` 的颜色来改变下划线的颜色。在低版本中,可以使用 `android:background` 属性将背景设置为空,然后使用 `android:drawableBottom` 属性添加下划线。 例如: ```xml <EditText android:background="@null" android:drawableBottom="@drawable/line" android:hint="请输入您的手机号码" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 其中,`@drawable/line` 是一个 shape drawable 文件,用于绘制下划线。 ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/colorBlue" /> <size android:height="1dp" android:width="1000dp" /> </shape> ``` 方法二:自定义 EditText 可以继承 `EditText` 类并重写 `onDraw` 方法来绘制下划线。 ```java 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.drawLine(0, this.getHeight() - 2, this.getWidth() - 2, this.getHeight() - 2, paint); } } ``` 需要注意的是,继承 `android.support.v7.widget.AppCompatEditText` 可能会出现获取不到焦点的问题。 实现 EditText 添加下划线可以使用两种方法:使用 XML 属性或自定义 EditText。前者更简单易用,而后者提供了更多的自定义选项。无论选择哪种方法,都是可以实现 EditText 添加下划线的。




























- 粉丝: 11
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于python的视频和文字聊天程序.zip
- 基于Python的算法题公式化套路总结.zip
- 基于Python的商品推荐系统.zip
- 基于python的算法可视化分析.zip
- 基于python的双目立体视觉及三维重建.zip
- 基于Python的图书管理系统.zip
- 基于python的图像标注小工具.zip
- 基于python的图书推荐系统.zip
- 基于Python的图形化tkinter简易学生成绩管理系统.zip
- 基于Python的网易云数据分析可视化大屏,Django框架.zip
- 基于Python的推荐系统学习记录.zip
- 基于python的微信公众号文章爬虫.zip
- 基于python的文件监控,使用于linux.zip
- 基于python的微信公众平台应用开发,代码托管在SAE上。.zip
- 基于Python的无人载具_无人船上位机控制系统可导入坐标自动巡航,带语音反馈功能.zip
- 基于python的无人驾驶小车.zip


