我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
当前回答
如果您有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);
}
}
其他回答
如果您有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);
}
}
更改Edittext的下划线颜色:
如果你想让整个应用程序共享这种风格,那么你可以这样做。
(1)转到styles.xml文件。你的AppTheme继承theme . appcompon . 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行颜色的任何颜色。
如果你有其他style.xml的值文件夹,这一步是非常重要的。因为这个文件将继承你之前的父xml文件。例如,我有values-19/styles.xml。这是专门为奇巧和以上。将它的父类更改为AppBaseTheme,并确保去掉“colorAccent”,这样它就不会覆盖父类的颜色。此外,您还需要保留特定于版本19的项。然后它会是这样的。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
你可以通过着色背景来改变颜色 < EditText android: backgroundTint = " @color /红" / >
线条的颜色由EditText的background属性定义。要改变它,你应该改变布局文件中的android:background。
我应该注意的是,这种风格是通过使用9个补丁绘制来实现的。如果你查看SDK,你可以看到EditText的背景是这样的图片:
要改变它,你可以在图像处理程序中打开它,并将其涂成所需的颜色。保存为bg_edit_text.9.png,然后把它放在你的可绘制文件夹。现在你可以像这样应用它作为你的EditText的背景:
android:background="@drawable/bg_edit_text"
为该编辑文本使用android:background属性。将可绘制的文件夹图像传递给它。 例如,
android:background="@drawable/abc.png"