Android更改EditText下划线颜色样式的方法
原创
©著作权归作者所有:来自51CTO博客作者痛而不觉的原创作品,请联系作者获取转载授权,否则将追究法律责任
相信大家都知道,当使用AppCompatEditText(Edit Text)时,默认的下划线是跟随系统的#FF4081的颜色值的,通过改变这个值可以改变所有的颜色样式
有时候你想单独定义某一个界面的颜色样式,则可以这样做:
1.在你的build.gradle中添加最新的appcompat库
1 2 3
|
dependencies { compile 'com.android.support:appcompat-v7:X.X.X' // X.X.X 为最新的版本号 }
|
2.让你的activity继承android.support.v7.app.AppCompatActivity
1 2 3
|
public class MainActivity extends AppCompatActivity { ... }
|
3.在任何layout.xml文件中声明您的EditText
1 2 3 4
|
< EditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:hint = "Hint text" />
|
4.在styles.xml文件中声明自定义样式
1 2 3 4
|
< style name = "MyEditText" parent = "Theme.AppCompat.Light" > < item name = "colorControlNormal" >@color/indigo</ item > < item name = "colorControlActivated" >@color/pink</ item > </ style >
|
5.通过android:theme属性将此样式应用于您的EditText
1 2 3 4 5
|
<EditText android:layout_width= "match_parent" android:layout_height= "wrap_content" android:hint= "Hint text" android:theme= "@style/MyEditText" />
|
效果如下:

总结
以上就是关于Android更改EditText下划线颜色样式的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。