【问题标题】:How to change bottom line color of EditText?如何更改 EditText 的底线颜色?
【发布时间】:2016-05-19 20:38:45
【问题描述】:

我正在尝试更改 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>

但我的底线仍然是黑色的。

做什么?

【问题讨论】:

标签: java android android-edittext


【解决方案1】:

使用:

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

注意:getColor() 方法已被 API 级别 23 弃用。所以你需要使用 Contextcompat 来获取颜色。

【讨论】:

  • 你需要用import android.graphics.PorterDuff;来导入
【解决方案2】:

试试这个答案: 改变你的 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>

【讨论】:

【解决方案3】:

创建一个可绘制的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

android:background="@drawable/edit"

现在你的底线会改变

9 个补丁图像

【讨论】:

    【解决方案4】:

    如果您使用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);
    

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2021-11-09
      • 2017-07-23
      相关资源
      最近更新 更多