如何改变EditText的底线颜色?

时间:2016-05-19 12:08:44

标签: java android android-edittext

我正在尝试更改EditText下的底线颜色。我可以看到像我一样的问题,但没有任何帮助。这是我的xml:

<EditText
        android:id="@+id/loginEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"         
        android:hint="@string/login"
         android:theme="@style/EditTextStyle"
        android:inputType="textPersonName" >

这是我的风格:

 <resources>
    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
       <item name="colorPrimary">@color/material_blue_500</item>
       <item name="colorPrimaryDark">@color/material_blue_700</item>
       <item name="colorAccent">@color/material_blue_700</item>  
       <item name="colorControlNormal">@color/material_blue_700</item>
       <!-- Active thumb color & Active track color(30% transparency) -->
       <item name="colorControlActivated">@color/material_blue_500</item>
       <!-- Inactive thumb color -->
       <item name="colorSwitchThumbNormal">@color/lightGrey</item>
       <!-- Inactive track color(30% transparency) -->
       <item name="android:colorForeground">@color/grey</item>   
       <item name="android:editTextStyle">@style/EditTextStyle</item>
       <item name="colorControlNormal">@color/material_blue_400</item>
       <item name="colorControlActivated">@color/material_blue_700</item>
       <item name="colorControlHighlight">@color/material_blue_700</item>

    </style>

    <style name="EditTextStyle" parent="Widget.AppCompat.EditText">
             <item name="colorControlNormal">@color/material_blue_400</item>
             <item name="colorControlActivated">@color/material_blue_700</item>
             <item name="colorControlHighlight">@color/material_blue_700</item>
    </style>
    </resources> 

这是我的颜色:

<color name="green">#00FF00</color>
<color name="thumbColor">#66aaaaaa</color>
<color name="darkGrey">#888</color>
<color name="grey">#ccc</color>
<color name="white">#fff</color>
<color name="lightGrey">#bbb</color>
<color name="veryMidGrey">#eeeeee</color>
<color name="veryLightGrey">#efefef</color>
<color name="transparent">#00000000</color>
<color name="whiteTransparent">#CCFFFFFF</color>
<color name="blackTransparent">#CC000000</color>
<color name="material_blue_800">#1565C0</color>
<color name="material_blue_500">#2196F3</color>
<color name="material_blue_400">#42A5F5</color>
<color name="material_blue_700">#1976D2</color>
<color name="material_blue_300"> #64B5F6</color>
<color name="black">#000</color>

但我的底线仍然是黑色。

@Autowired

做什么?

4 个答案:

答案 0 :(得分:1)

使用:

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

注意:对于API级getColor(),不推荐使用23方法。因此,您需要使用 Contextcompat 来获取颜色。

答案 1 :(得分:0)

试试这个答案: 在style.xml中更改

<style name="Theme.App.Base" parent="Widget.AppCompat.EditText">
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">@color/accent</item>
<item name="colorControlHighlight">@color/accent</item>
</style>

答案 2 :(得分:0)

创建一个drawable edit.xml

<?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item android:state_activated="true" android:drawable="@drawable/apptheme_textfield_activated_holo_light" />
            <item android:state_focused="true" android:drawable="@drawable/apptheme_textfield_activated_holo_light" />
            <item android:state_activated="false" android:drawable="@drawable/apptheme_textfield_focused_holo_light" />

        </selector>

将此drawable应用于editText

机器人:背景=&#34; @可绘制/编辑&#34;

现在你的底线会改变

9个补丁图片

apptheme_textfield_activated_holo_light apptheme_textfield_activated_holo_light

答案 3 :(得分:0)

  

如果您使用appcompat-v7:22.1.0+使用Drawable Compat

参考此功能。

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

并应用于您的观点。

 EditText txtView = (EditText) findViewById(R.id.edtCurrency);
 tintColorWidget(txtView, R.color.colorPrimary);