我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
当前回答
最好的方法是使用AppCompatEditText和应用命名空间的backgroundTint属性。即。
<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级别的应用程序。
其他回答
在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));
它非常简单(要求:最低API 21)…
转到xml并选择EditText字段 在右侧,你可以看到“Attributes”窗口。选择“查看所有属性” 只要搜索“tint” 添加/更改backgroundTint为所需的十六进制颜色(例如#FF0000)
保持编码……:)
您可以通过编程方式更改EditText的颜色,只需使用以下代码行:
如果您想要一条平线,可以使用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>
如果要为聚焦的编辑文本提供不同的宽度和颜色,则使用选择器替换形状。
我不喜欢之前的答案。最好的解决方案是使用:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
android:backgroundTint for EditText仅适用于API21+。因此,我们必须使用支持库和AppCompatEditText。
注意:我们必须使用app:backgroundTint而不是android:backgroundTint
AndroidX版本
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />