我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
当前回答
这是最好的工具,你可以使用所有的观点,它的免费许多感谢@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" />
其他回答
为该编辑文本使用android:background属性。将可绘制的文件夹图像传递给它。 例如,
android:background="@drawable/abc.png"
如果您有edittext的自定义类,则可以动态地执行此操作。
首先,你必须声明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);
}
}
在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));
通过编程,您可以尝试:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
你也可以通过像这样对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" />