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


当前回答

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

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

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

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

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

其他回答

添加app:backgroundTint低于api级别21。否则使用android:backgroundTint。

适用于api级别21以下。

<EditText
     android:id="@+id/edt_name"
     android:layout_width="300dp"
     android:layout_height="wrap_content"
     android:textColor="#0012ff"
     app:backgroundTint="#0012ff"/>

适用于api等级21以上。

<EditText
     android:id="@+id/edt_name"
     android:layout_width="300dp"
     android:layout_height="wrap_content"
     android:textColor="#0012ff"
     android:backgroundTint="#0012ff"/>

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

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

Use:

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

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

公认的答案是更基于每个样式的东西,但最有效的做法是在你的AppTheme样式中添加colorAccent属性,像这样:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:editTextStyle">@style/EditTextStyle</item>
</style>

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

colorAccent属性用于在整个应用程序中为小部件着色,因此应该用于一致性

如果你想改变底线而不使用应用程序的颜色,在你的主题中使用这些行:

<item name="android:editTextStyle">@android:style/Widget.EditText</item>
<item name="editTextStyle">@android:style/Widget.EditText</item>

我不知道还有别的办法。