以编程方式更改Android EditText的色调颜色

Mor*_*itz 24 android

我试图EditText在运行时以编程方式更改View 的着色颜色.基本上我想改变你通常应用的内容,?attr/colorControlNormal就像在默认背景drawable中一样.

new ColorsStateList使用一种颜色设置更改背景色调无法正确应用:

editText.setBackgroundTintList( ColorStateList.valueOf( color ) );

首先,EditText虽然应用了色调列表并且内部改变了drawable,但结果将应用于所有结果.此外,默认背景1中指定的alpha 在开头可见.

这是在第一个上设置色调颜色的结果EditText:

仅在第一个EditText上设置色调颜色的结果

所以我的问题是:如何以编程方式将色调正确应用于EditText

ing*_*sid 38

这对我有用:

editText.getBackground().setColorFilter(getResources().getColor(R.color.your_color),
                                        PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)

来源:使用appcompat v7更改EditText底线颜色


Mor*_*itz 20

通过新引入的android.support.v4.graphics.drawable.DrawableCompat#setTint设置,现在可以实现颜色.

  • 例如:`DrawableCompat.setTint(editText.getBackground(),ContextCompat.getColor(context,R.color.xxx));` (5认同)

Len*_*ick 9

尝试创建自定义EditText并添加this.setBackgroundTintList( ColorStateList.valueOf( color ) );到构造函数中.


t0m*_*t0m 6

setColorFilter不为我工作。我用了:

Drawable wrappedDrawable = DrawableCompat.wrap(mView.getBackground());
DrawableCompat.setTint(wrappedDrawable, getResources().getColor(R.color.red));
mView.setBackgroundDrawable(wrappedDrawable);
Run Code Online (Sandbox Code Playgroud)

或者

DrawableCompat.setTint(mView.getBackground(), ContextCompat.getColor(this, R.color.red));
Run Code Online (Sandbox Code Playgroud)

咱们试试吧。