Android定制自己的EditText轻松改变底线颜色

所属分类: 软件编程 / Android 阅读数: 1567
收藏 0 赞 0 分享

最近做 android 项目遇到这个问题,为了保持 app 风格一致,需要将原生的EditText底线颜色改成橙色。网上搜了一些解决方案,特此记录总结一下。

效果图

默认的EditText底线颜色是蓝色的,

我们想实现橙色的效果

实现方法

1、准备两个背景图

一个作为 edittext 的默认背景 , 另一个作为 输入时候的背景

Note

使用 9.png, 不要用png, 否则图片会模糊, 花掉

在文件夹 drawable 用selector 建立一个xml 文件

<!-- drawable/edittext_shape.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">

 <item android:drawable="@drawable/edittext_default" android:state_focused="false"/>
 <item android:drawable="@drawable/edittext_focused" android:state_focused="true"/>

</selector>

在 values 文件夹 下面的 styles.xml 新建一个style

此步骤是为了复用这个样式, 也可以不用style, 直接在 layout里的布局 xml 里 写代码

<!-- drawable/values/styles.xml -->

<style name="SmsEditText">
 <item name="android:layout_marginLeft">20dp</item>
 <item name="android:layout_marginRight">20dp</item>
 <item name="android:layout_marginTop">20dp</item>
 <item name="android:layout_width">match_parent</item>
 <item name="android:layout_height">wrap_content</item>
 <item name="android:textSize">16sp</item>
 <item name="android:background">@drawable/edittext_shape</item>
</style>

在layout的布局文件中引用定制的edittext

<!-- drawable/layout/fragment_bomb.xml -->
<LinearLayout
 android:id="@+id/input"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:padding="20dp" >

 <EditText
  android:id="@+id/phone"
  style="@style/SmsEditText"
  android:hint="@string/phone_hint"
  android:inputType="phone"
  android:maxLength="11"
  android:maxLines="1" />

 <EditText
  android:id="@+id/times"
  style="@style/SmsEditText"
  android:hint="@string/times_hint"
  android:inputType="number"
  android:maxLines="1" />
</LinearLayout>

在edittext 底部加上一条直线( 仿微信)

原生的效果是edittext底部是一个凹形的线,这样不是很美观。微信的输入框下面是一条直线。如何实现呢?可以将上面的图片改成直线型的,不过需要美工人员 PS 的帮忙。我们也可以利用 xml 文件来画出图形,完成所需的直线效果。

利用xml 画线

本来想利用xml 画线, 做出微信 输入框 的那种下面是一条直线,发现纯粹用xml不美观, 这种还是让美工做一个背景图可能比较好。

查看这篇文章:android利用xml实现分割线

edittext 去除边框

android:background="@null"

这个代码可以去掉 edittext 的边框
edittext 底部加线

在drawable 新建一个 line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

 <solid android:color="@color/orange_normal" />

 <size
  android:height="1dp"
  android:width="1000dp" />

</shape>

在layout 的布局文件中引用

<EditText
  android:id="@+id/phone"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@null"
  android:drawableBottom="@drawable/line"
  android:hint="@string/phone_hint"
  android:inputType="phone"
  android:maxLength="11"
  android:maxLines="1" />

以上就是本文的全部内容,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多