文本下方的线条是设置为 View 背景的可绘制对象,当调用 editText.setBackgroundColor(color)
时,内部会发生以下情况:
setBackground(new ColorDrawable(color));
这会删除包含线条的可绘制对象并将其替换为我们为其指定的颜色。
是否可以更改 EditText 的背景颜色而不使文本下方的线条消失?
在布局中设置 EditText 并更改布局的背景颜色不是一种选择。
最佳答案
试着让 drawable 像这样思考:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-2dp" android:left="-2dp" android:right="-2dp">
<shape>
<solid android:color="@color/background_color"/>
<stroke android:color="@color/underline_color" android:width="2dp"/>
</shape>
</item>
</layer-list>
从这样的资源中设置您的 EditText 背景:
editText.setBackgroundResource(R.drawable.name_to_xml_file);
关于java - 更改 EditText 背景颜色并保留下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099223/