[英]How to change line color in EditText
我正在我的布局 xml 文件中创建一个 EditText
但我想将 EditText 中的颜色线从 Holo 更改为(例如)红色。 那怎么办?
这是您可以用于所有视图的最佳工具,并且非常感谢@ Jérôme Van Der Linden 。
Android Holo Colors Generator 允许您轻松地为您的 Android 应用程序使用您自己的颜色创建 Android 组件,例如EditText
或微调器。 它将生成所有必需的九个补丁资产以及相关的 XML drawable 和样式,您可以将它们直接复制到您的项目中。
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" />
android:backgroundTint
for EditText
仅适用于API21+ 。 因此,我们必须使用支持库和AppCompatEditText
。
注意:我们必须使用app:backgroundTint
而不是android:backgroundTint
安卓X版本
<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);
我认为最好的方法是按主题:
<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 的颜色: edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
要更改 Edittext 的下划线颜色:
如果你想让整个 App 都共享这种风格,那么你可以通过以下方式进行。
(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)如果你有其他带有style.xml的values文件夹,这一步很重要。 因为该文件将从您以前的父 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
。
我应该注意到这种风格是通过使用 9-patch drawable 实现的。 如果查看SDK,可以看到EditText
的背景是这张图片:
要更改它,您可以在图像处理程序中打开它并将其着色为所需的颜色。 将其另存为bg_edit_text.9.png
,然后将其放入可绘制文件夹中。 现在您可以将其应用为EditText
的背景,如下所示:
android:background="@drawable/bg_edit_text"
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>
设置为编辑文本
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext"/>
小部件的背景取决于 API 级别。
备选方案 1
您可以通过以下方式为EditText
背景提供自定义图像
android:background="@drawable/custom_editText"
您的图像应该看起来像这样。 它会给你想要的效果。
备选方案 2
将此 xml 设置为您的EditText
背景属性。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#4C000000"/>
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
这将在每个 API 上与您的EditText
具有相同的外观和感觉。
您可以通过给背景着色来更改颜色<EditText android:backgroundTint="@color/red"/>
使用此方法..并根据您的视图名称进行修改。 这段代码效果很好。
private boolean validateMobilenumber() {
if (mobilenumber.getText().toString().trim().isEmpty() || mobilenumber.getText().toString().length() < 10) {
input_layout_mobilenumber.setErrorEnabled(true);
input_layout_mobilenumber.setError(getString(R.string.err_msg_mobilenumber));
// requestFocus(mobilenumber);
return false;
} else {
input_layout_mobilenumber.setError(null);
input_layout_mobilenumber.setErrorEnabled(false);
mobilenumber.setBackground(mobilenumber.getBackground().getConstantState().newDrawable());
}
如果你想要一条扁平的线,你可以用 xml 轻松地做到这一点。 这是 xml 示例:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-1dp"
android:left="-1dp"
android:right="-1dp"
android:bottom="1dp"
>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#6A9A3A"/>
</shape>
</item>
</layer-list>
如果您想为焦点编辑文本提供不同的宽度和颜色,请用选择器替换该形状。
最好的方法是使用AppCompatEditText
和app
命名空间的backgroundTint
属性。 IE
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
app:backgroundTint="YOUR COLOR"
android:layout_height="wrap_content" />
当我们使用android:backgroundTint
它只能在 API21 或更高版本中工作,但app:backgroundTint
可以在您的应用程序执行的所有 API 级别上工作。
为该编辑文本使用 android:background 属性。 将您的可绘制文件夹图像传递给它。 例如,
android:background="@drawable/abc.png"
<EditText
android:id="@+id/et_password_tlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#9e9e9e"
android:backgroundTint="#000"
android:singleLine="true"
android:drawableTint="#FF4081"
android:paddingTop="25dp"
android:textColor="#000"
android:paddingBottom="5dp"
android:inputType="textPassword"/>
<View
android:id="@+id/UnderLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/et_password_tlay"
android:layout_centerHorizontal="true"
android:background="#03f94e" />
**这是使用视图的一种方式**
尝试以下方法,当用作背景属性时,它将转换 EditText 的底线颜色。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="@dimen/spacing_neg"
android:right="@dimen/spacing_neg"
android:top="@dimen/spacing_neg">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="@dimen/spacing_1"
android:color="@android:color/black" />
</shape>
</item>
</layer-list>
如果您有用于 edittext 的自定义类,则可以动态执行此操作。
首先,您已经声明了下面给出的编辑文本的状态和颜色。
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
然后用它创建 ColorStateList 变量
ColorStateList myList = new ColorStateList(states, colors);
然后最后一步是将其分配给edittext。
editText.setBackgroundTintList(myList);
在此之后,您必须编写重点更改事件。
this.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
setUnderlineColor(selectionColor,deselectionColor);
}
});
你可以在 setUnderlineClor() 方法中制作上面的代码,
private void setUnderlineColor(int primaryColor, int secondaryColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
ColorStateList myList = new ColorStateList(states, colors);
setBackgroundTintList(myList);
}
}
这可以简单地通过包含这个android:theme="@style/AppTheme.AppBarOverlay
作为你的 editText 的属性并添加这个<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
你的风格
在 xml 布局中使用:
android:backgroundTint="@color/colorPrimary"
或者在java代码中复制这个方法:
public void changeLineColorInEditText(EditText editText, int color) {
editText.setBackgroundTintList(ColorStateList.valueOf(color));
}
并像这样使用它:
changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.