我使用appcompat v7,以获得在Android 5和更少的外观一致。它运行得相当好。但是,我不知道如何更改EditTexts的底线颜色和强调色。这可能吗?

我试图定义一个自定义android:editTextStyle(参见下面),但我只成功地改变了完整的背景色或文本颜色,但不是底线或强调色。是否有要使用的特定属性值?我必须通过android:background属性使用自定义可绘制图像吗?在六边形中不能指定颜色吗?

 <style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
     <item name="android:editTextStyle">@style/Widget.App.EditText</item>
 </style>

 <style name="Widget.App.EditText" parent="Widget.AppCompat.EditText">
     ???
 </style>

根据android API 21的来源,材料设计的EditTexts似乎使用colorControlActivated和colorControlNormal。因此,我尝试在前面的样式定义中覆盖这些属性,但没有效果。可能appcompat不使用它。不幸的是,我找不到最新版本的appcompat材料设计的来源。


当前回答

虽然Laurents的解决方案是正确的,但它也有一些缺点,正如评论中所描述的,因为不仅编辑文本的底线会被着色,而且工具栏的后退按钮,复选框等也会被着色。

幸运的是,appcompat-v7的v22.1版本引入了一些新的可能性。现在可以将特定的主题仅分配给一个视图。直接来自更新日志:

不赞成使用应用程序:主题样式工具栏。你现在可以在所有API级别7及更高的设备上使用android:theme工具栏,并在API级别11及更高的设备上使用android:theme支持所有小部件。

因此,我们不是在全局主题中设置所需的颜色,而是创建一个新的主题并仅将其分配给EditText。

例子:

<style name="MyEditTextTheme">
    <!-- Used for the bottom line when not selected / focused -->
    <item name="colorControlNormal">#9e9e9e</item>
    <!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/MyEditTextTheme"/>

其他回答

可以在XML中使用以下命令更改:

参考API >= 21兼容性使用:

android:backgroundTint="@color/blue"

对于向后API < 21的兼容性使用:

app:backgroundTint="@color/blue"

我觉得这需要一个答案,以防有人想要改变一个编辑文本。我是这样做的:

editText.getBackground().mutate().setColorFilter(ContextCompat.getColor(context, R.color.your_color), PorterDuff.Mode.SRC_ATOP);

非常简单,只需在EditText中添加android:backgroundTint属性。

android:backgroundTint="@color/blue"
android:backgroundTint="#ffffff"
android:backgroundTint="@color/red"


 <EditText
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:backgroundTint="#ffffff"/>

下面是TextInputLayout在支持设计库中的部分源代码(更新为23.2.0版本),它以更简单的方式改变EditText的底线颜色:

private void updateEditTextBackground() {
    ensureBackgroundDrawableStateWorkaround();

    final Drawable editTextBackground = mEditText.getBackground();
    if (editTextBackground == null) {
        return;
    }

    if (mErrorShown && mErrorView != null) {
        // Set a color filter of the error color
        editTextBackground.setColorFilter(
                AppCompatDrawableManager.getPorterDuffColorFilter(
                        mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
    }
    ...
}

在23.2.0中,如果您想通过编程方式更改颜色,上述所有代码似乎都变得毫无用处。

如果你想支持所有平台,以下是我的方法:

/**
 * Set backgroundTint to {@link View} across all targeting platform level.
 * @param view the {@link View} to tint.
 * @param color color used to tint.
 */
public static void tintView(View view, int color) {
    final Drawable d = view.getBackground();
    final Drawable nd = d.getConstantState().newDrawable();
    nd.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(
            color, PorterDuff.Mode.SRC_IN));
    view.setBackground(nd);
}

我用这个方法改变了线条的颜色与PorterDuff,没有其他的绘图。

public void changeBottomColorSearchView(int color) {
    int searchPlateId = mSearchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    View searchPlate = mSearchView.findViewById(searchPlateId);
    searchPlate.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}