如何将边框添加到视图的特定边

时间:2017-07-12 07:33:29

标签: java android view border

这是我向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。

2 个答案:

答案 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" />