我试图在运行时以编程方式更改 EditText
View的着色颜色 . 基本上我想改变你通常应用的 ?attr/colorControlNormal
,如default background drawable .
仅使用一种颜色设置 new ColorsStateList
,无法正确应用更改背景色调:
editText.setBackgroundTintList( ColorStateList.valueOf( color ) );
首先,结果应用于所有 EditText
,尽管应用了色调列表并在内部改变了drawable . 此外,默认背景1中指定的alpha在开头可见 .
这是在第一个 EditText
上设置色调颜色的结果:
所以我的问题是:如何以编程方式将色调正确应用于 EditText
?
4 回答
使用新引入的
android.support.v4.graphics.drawable.DrawableCompat#setTint
设置,现在可以使用颜色 .尝试创建自定义
EditText
并将this.setBackgroundTintList( ColorStateList.valueOf( color ) );
添加到构造函数中 .这对我有用:
资料来源:Changing EditText bottom line color with appcompat v7
我写了一个小组件来实现这种行为 .
几个重要的注意事项:
使用旧学校
setColorFilter
方法要使色调工作,首先将焦点切换到其他视图,然后着色
EditText
background drawable用法
XML
资源
在带有
com.android.support:appcompat-v7:22.1.1
库的API 16/21上进行了测试 .