这就是我如何向EditText
. 如何仅在 a 的 3 边添加边框EditText
,并为每个边框定义不同的颜色和宽度?
EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); // white background
border.setStroke(1, 0xFF000000); // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackgroundDrawable(border);
} else {
editText.setBackground(border);
}
我在下面的尝试不起作用:
<?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="#FF0000" />
</shape>
</item>
<item android:right="5dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF" />
</shape>
</item>
<item android:left="22dp">
<shape android:shape="rectangle">
<solid android:color="#746565" />
</shape>
</item>
</layer-list>
提前致谢。
创建一个可绘制的图层列表并定义 3 个具有自定义宽度和颜色的不同矩形形状,并隐藏每个形状的 3 个边以仅显示一侧。像这样:
my_edittext_border.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list android:paddingLeft="30dp" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-1dp"
android:left="-1dp"
android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#ff0000" />
</shape>
</item>
<item
android:bottom="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="3dp"
android:color="#11ee66" />
</shape>
</item>
<item
android:bottom="-5dp"
android:left="-5dp"
android:top="-5dp">
<shape android:shape="rectangle">
<stroke
android:width="5dp"
android:color="#0000ff" />
</shape>
</item>
</layer-list>
现在将此 drawable 设置为您的EditText
背景:
android:background="@drawable/my_edittext_border"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句