我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
当前回答
可拉的/ 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>
设置为EditText
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext"/>
其他回答
使用这个方法..并根据视图名称修改它。这段代码工作得很好。
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());
}
这是最好的工具,你可以使用所有的观点,它的免费许多感谢@Jérôme Van Der Linden。
Android Holo颜色生成器允许您轻松地创建Android组件,如EditText或旋转与您自己的Android应用程序的颜色。它将生成所有必要的9个补丁资产加上相关的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" />
如果您想要一条平线,可以使用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>
如果要为聚焦的编辑文本提供不同的宽度和颜色,则使用选择器替换形状。
它非常简单(要求:最低API 21)…
转到xml并选择EditText字段 在右侧,你可以看到“Attributes”窗口。选择“查看所有属性” 只要搜索“tint” 添加/更改backgroundTint为所需的十六进制颜色(例如#FF0000)
保持编码……:)
为该编辑文本使用android:background属性。将可绘制的文件夹图像传递给它。 例如,
android:background="@drawable/abc.png"