如何把一个android的relativelayout边框?

我已经看到这个围绕android textview的边框,我用它。 但是现在,我想在一个相对布局的小部件周围放置一个边框。 我该怎么做?

  1. res/drawable文件夹中,创build一个新文件background_border.xml

在这个文件中,你将像这样定义小部件的背景:

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- This is the stroke you want to define --> <stroke android:width="1dp" android:color="@color/color_stroke"/> <!-- Optional, round your corners --> <corners android:bottomLeftRadius="0dp" android:topLeftRadius="5dp" android:bottomRightRadius="5dp" android:topRightRadius="0dp" /> <!-- Optional, fill the rest of your background with a color or gradient, use transparent if you only want the border to be displayed--> <gradient android:startColor="@android:color/transparent" android:endColor="@android:color/transparent" android:angle="90"/> </shape> 
  1. 将小部件的背景设置为刚创build的可绘制configuration

例如。 如果你想把你的边界放在一个相关的布局上:

 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/background_border" android:padding="15dp"> ... </RelativeLayout> 
 RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.borderEffect); // id fetch from xml ShapeDrawable rectShapeDrawable = new ShapeDrawable(); // pre defined class // get paint Paint paint = rectShapeDrawable.getPaint(); // set border color, stroke and stroke width paint.setColor(Color.GRAY); paint.setStyle(Style.STROKE); paint.setStrokeWidth(5); // you can change the value of 5 layout.setBackgroundDrawable(rectShapeDrawable); 

创build一个FrameLayout,获取边框的背景颜色,以及边框宽度的边距或边距,然后将该FrameLayout放置在RelativeLayout中。 将TextView放在FrameLayout中,而不是直接放在RelativeLayout中。 噗的瞬间边界。

虽然所有提供的答案的工作,他们是非常刚性的。如果你想要自定义边框颜色,borderthickickness不同的屏幕。 为此,您应该尝试我的解决scheme。我们将按照三个步骤创build一个自定义的RelativeLayout,使您可以为下边框提供borderColor和Thickness。

1)创build一个扩展RelativeLayout的类并覆盖Draw方法

 public class BorderRelativeLayout extends RelativeLayout { private float borderThickness; private int borderColor; public BorderRelativeLayout(Context context) { this(context, null, 0); } public BorderRelativeLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BorderRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect = new Rect(); Paint paint = new Paint(); paint.setColor(borderColor); paint.setStrokeWidth(borderThickness); getLocalVisibleRect(rect); canvas.drawLine(rect.left, rect.bottom, rect.right, rect.bottom, paint); } private void init(Context context, AttributeSet attrs) { setWillNotDraw(false); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BorderRelativeLayout); borderThickness = array.getDimension(R.styleable.BorderRelativeLayout_borderThickness, 0.5f); borderColor = array.getColor(R.styleable.BorderRelativeLayout_borderColor, ContextCompat.getColor(context, R.color.colorPrimary)); } } 

2)在attrs.xml中定义可调整的属性

 <declare-styleable name="BorderRelativeLayout"> <attr name="borderThickness" format="dimension"/> <attr name="borderColor" format="color"/> </declare-styleable> 

3)你已经完成,你可以使用它

 <com.spacewek.spacewek.controls.BorderRelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/headLayout" app:borderThickness="2dp" app:borderColor="@color/divider_new_color"> </com.spacewek.spacewek.controls.BorderRelativeLayout>