Mil*_*ank 268
这是您可以用于所有视图的最佳工具,非常感谢@ JérômeVanDer Linden.
Android Holo颜色生成器允许您轻松地EditText
为Android应用程序创建具有自己颜色的Android组件,例如旋转器或微调器.它将生成所有必需的九个补丁资源以及相关的XML drawable和样式,您可以将它们直接复制到项目中.
更新1
这个域似乎已过期,但项目是开源的,你可以在这里找到
试试吧
这个图像放在背景中 EditText
android:background="@drawable/textfield_activated"
Run Code Online (Sandbox Code Playgroud)
更新2
对于API 21或更高版本,您可以使用 android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="@android:color/holo_red_light" />
Run Code Online (Sandbox Code Playgroud)
立即更新3我们有支持AppCompatEditText
注意:我们需要使用app:backgroundTint而不是android:backgroundTint
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
app:backgroundTint="@color/blue_gray_light" />
Run Code Online (Sandbox Code Playgroud)
Mla*_*jac 181
我不喜欢以前的答案.最好的解决方案是使用:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
Run Code Online (Sandbox Code Playgroud)
android:backgroundTint for EditText仅适用于API21 +.因此,我们必须使用支持库和AppCompatEditText.
注意:我们必须使用app:backgroundTint而不是android:backgroundTint.
小智 71
您还可以通过对EditText的背景进行着色来快速更改EditText的下划线颜色,如下所示:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Something or Other"
android:backgroundTint="@android:color/holo_green_light" />
Run Code Online (Sandbox Code Playgroud)
doc*_*ram 20
以编程方式,您可以尝试:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)
Rah*_*hul 17
对于21以下的api,您可以使用edittext中的theme属性将下面的代码放入样式文件中
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在edittext中使用此样式
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="@dimen/user_input_field_height"
android:layout_marginTop="40dp"
android:hint="@string/password_hint"
android:theme="@style/MyEditTextTheme"
android:singleLine="true" />
Run Code Online (Sandbox Code Playgroud)
j2e*_*nue 11
我认为最好的方式是主题:
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/black</item>
<item name="colorControlActivated">@color/action_blue</item>
<item name="colorControlHighlight">@color/action_blue</item>
</style>
<style name="AddressBookStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">13sp</item>
<item name="android:theme">@style/MyEditTextTheme</item>
</style>
<android.support.v7.widget.AppCompatEditText
style="@style/AddressBookStyle"/>
Run Code Online (Sandbox Code Playgroud)
mat*_*ani 10
您可以使用以下代码行以编程方式更改 EditText 的颜色:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
小智 9
要更改Edittext的下划线颜色:
如果您希望整个应用程序共享此样式,则可以按以下方式执行.
(1)转到styles.xml文件.继承Theme.AppCompat.Light.DarkActionBar(在我的例子中)的父级的AppTheme将是应用程序中所有样式文件的基本父级.将其名称更改为"AppBaseTheme".在其下创建一个名为AppTheme的另一种样式,并继承自您刚编辑的AppBaseTheme.它将如下所示:
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
<item name="colorPrimary">@color/material_brown_500</item>
<item name="colorPrimaryDark">@color/material_brown_700</item>
<item name="colorAccent">@color/flamingo</item>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
</style>
Run Code Online (Sandbox Code Playgroud)
然后将"colorAccent"更改为您希望EditText线颜色的颜色.
(2)如果你有style.xml的其他值文件夹,这一步非常重要.因为该文件将继承您以前的父xml文件.例如,我有值-19/styles.xml.这是专门针对Kitkat及以上的.将其父级更改为AppBaseTheme并确保删除"colorAccent",以便它不会覆盖父级的颜色.此外,您需要保留特定于版本19的项目.然后它将如下所示.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
它非常简单(必需:最低API 21)......
保持编码........ :)
线条的颜色由EditText
背景属性定义.要更改它,您应该更改android:background
布局文件.
我应该注意到这种风格是通过使用9-patch drawable来实现的.如果您查看SDK,可以看到EditText
该图像的背景是:
要更改它,您可以在图像处理程序中打开它并以所需的颜色着色.保存为bg_edit_text.9.png
,然后将其放在可绘制的文件夹中.现在您可以将它作为背景应用于您的EditText
喜好:
android:background="@drawable/bg_edit_text"
Run Code Online (Sandbox Code Playgroud)
drawable/bg_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/colorDivider" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
设置为编辑文本
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext"/>
Run Code Online (Sandbox Code Playgroud)
小智 5
在 xml 布局中使用:
android:backgroundTint="@color/colorPrimary"
Run Code Online (Sandbox Code Playgroud)
或者在java代码中复制这个方法:
public void changeLineColorInEditText(EditText editText, int color) {
editText.setBackgroundTintList(ColorStateList.valueOf(color));
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));
Run Code Online (Sandbox Code Playgroud)