AppCompat 和 EditText 下划线不同 API

AppCompat and EditText different underline on different API

我正在尝试为 EditText 更改下划线颜色(它将用于输入验证,因此它必须能够在运行时更改)。

我正在使用 AppCompat 库。问题是在 API 21 及以上,我看到透明黑线(灰色覆盖),而不是粗体版本。

如何让它和API16一样?

我使用此代码更改色调:

    final Drawable originalDrawable = view.getBackground();
    final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
    DrawableCompat.setTint(wrappedDrawable, Color.RED);
    setBackground(view,wrappedDrawable);

你不应该改变背景。最好创建一个主题,并使用主题颜色(colorPrimary、colorAccent 对小部件最重要)来获得所需的效果。将主题分配给您的 EditText 并享受。注意:您应该使用 AppCompat 主题之一作为基本主题。

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorAccent">@color/accent</item>
</style>

在你的 colors.xml

<color name="primary">#ff0000</color>
<color name="accent">#00ff00</color>

通过将这些行添加到我的主题找到的解决方案:

    <item name="editTextStyle">@style/Base.V7.Widget.AppCompat.EditText</item>
    <item name="editTextBackground">@drawable/abc_edit_text_material</item>