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


当前回答

我用这个方法改变了线条的颜色与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);
}

其他回答

虽然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"/>

在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=颜色为您想要的颜色

这是所有api中最简单、最有效/可重用/有效的 创建一个自定义EditText类,如下所示:

public class EditText extends android.widget.EditText {
    public EditText(Context context) {
        super(context);
        init();
    }

    public EditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        getBackground().mutate().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorAccent), PorterDuff.Mode.SRC_ATOP);
    }
}

然后这样使用它:

 <company.com.app.EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"/>

添加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"/>

公认的答案是更基于每个样式的东西,但最有效的做法是在你的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属性用于在整个应用程序中为小部件着色,因此应该用于一致性