我使用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材料设计的来源。


当前回答

Use:

<EditText
    app:backgroundTint="@color/blue"/>

这将支持前棒棒糖设备不仅+21

其他回答

在activi . xml中添加代码

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText"
        android:hint="Informe o usuário"
        android:backgroundTint="@android:color/transparent"/>

哪里BackgroundTint=颜色为您想要的颜色

下面是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);
}

如果你正在使用appcompat- version:22.1.0+,你可以使用DrawableCompat来着色你的小部件

    public static void tintWidget(View view, int color) {
        Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
        DrawableCompat.setTint(wrappedDrawable.mutate(), getResources().getColor(color));
        view.setBackgroundDrawable(wrappedDrawable);
    }

我也被这个问题困扰了太久。

我需要一个既适用于v21以上版本也适用于v21以下版本的解决方案。

我最终发现了一个非常简单,也许不是理想但有效的解决方案:简单地在EditText属性中设置背景颜色为透明。

<EditText
    android:background="@android:color/transparent"/>

我希望这能为大家节省一些时间。

一个快速的解决方法是查看你的appspackage/build/intermediates/ explo- aar/com.android。支持/appcompat-v7/res/drawable/ abc_edit_text_material.xml并将该XML文件复制到drawable文件夹中。然后你可以在这个选择器中改变9个补丁文件的颜色,以匹配你的偏好。