这是我向EditText
添加边框的方法。如何才能在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);
}
Vielen dank im voraus。
答案 0 :(得分:1)
To get border on one side, you can create your own drawable like this:
<?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>
</layer-list>
And set this drawable as background to your EditText.
答案 1 :(得分:0)
If you want to add border only one side of EditText then you should use View tag in your layout file to draw a simple line and place it near your EditText and use background property to set color of line. To draw horizontal line use this:
<View
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="2dp" />