我在我的布局xml文件中创建了一个EditText。
但是,我想要将EditText中的线条颜色从Holo更改为(例如)红色。 如何实现呢?
这是你可以用于所有视图的最佳工具,并且完全免费,非常感谢@Jérôme Van Der Linden。
Android Holo Colors生成器允许您轻松创建Android组件,例如EditText
或下拉菜单,并使用自己的颜色为Android应用程序定制。它将生成所有必要的九片拉伸位图和相关的XML可绘制和样式,您可以直接复制到项目中。
http://android-holo-colors.com/
更新1
此域名似乎已过期,但该项目是开放源代码,您可以在此处找到
https://github.com/jeromevdl/android-holo-colors
试试吧
将此图像放在EditText
的背景中
android:background="@drawable/textfield_activated"
更新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" />
更新 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" />
更新4 AndroidX版本
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
我不喜欢之前的答案。最好的解决方案是使用:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
EditText
的 android:backgroundTint
只在 API21+ 上起作用,因此我们必须使用支持库以及 AppCompatEditText
。
注意:我们必须使用 app:backgroundTint
而不是 android:backgroundTint
AndroidX 版本
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
你也可以通过对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" />
对于21以下的API,您可以在EditText
中使用主题属性,将下面的代码放入样式文件中。
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
在 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" />
通过编程,您可以尝试:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
你可以通过以下代码来编程地改变EditText的颜色:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
我认为最好的方式是通过主题:
<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"/>
要改变Edittext的下划线颜色:
如果你想让整个应用程序共享此样式,则可以按照以下方式操作。
(1) 进入styles.xml文件。您的AppTheme继承了Theme.AppCompat.Light.DarkActionBar的父类(在我的情况下),将成为应用程序中所有样式文件的基本父类。将其名称更改为“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>
将“colorAccent”更改为您希望EditText行颜色的任何颜色。
(2) 如果您有其他values文件夹与style.xml,请注意以下步骤非常重要。因为该文件将从您之前的父xml文件继承。例如,我有一个values-19/styles.xml。这是专门为Kitkat及以上版本而设计的。将其父级更改为AppBaseTheme,并确保摆脱“colorAccent”,以使其不覆盖父级的颜色。还需要保留特定于版本19的项。然后它将看起来像这样。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
EditText
的背景属性定义。要更改它,您应该在布局文件中更改android:background
。EditText
的背景是这张图片:
要更改它,您可以在图像处理程序中打开它并将其涂上所需的颜色。将其保存为bg_edit_text.9.png
,然后将其放入drawable文件夹中。现在,您可以将其应用为EditText
的背景,如下所示:android:background="@drawable/bg_edit_text"