如何在Android RelativeLayout周围添加边框?

24

我看到了这个主题,关于在Android TextView周围放置边框的问题,我已经使用了这种方法。但现在,我想在位于RelativeLayout中的小部件周围放置边框。我该怎么做呢?

4个回答

61
  1. 在你的res/drawable文件夹中,创建一个名为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. 将您的小部件背景设置为刚创建的可绘制配置

例如,如果您想在RelativeLayout上放置边框:

<RelativeLayout            
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background_border"
            android:padding="15dp">
    ...
</RelativeLayout>

@color/color_stroke未定义。 - user10180461
@color/color_name 指向你在 res/values/colors.xml 资源文件中定义的颜色。 https://developer.android.com/guide/topics/resources/more-resources#Color @Dexter - RWIL

12
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);

1
虽然提供的所有答案都能起作用,但它们很死板。如果您想为不同的屏幕自定义边框颜色和边框厚度,那么您应该尝试我的解决方案。我们将遵循三个步骤创建一个自定义的RelativeLayout,允许您为底部边框提供borderColor和Thickness。
1)创建一个类,扩展RelativeLayout并覆盖on 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>

1
创建一个FrameLayout,获取您边框的背景颜色和边框宽度的边距或填充,并将该FrameLayout放置在RelativeLayout中。将TextView放置在FrameLayout中,而不是直接放置在RelativeLayout中。瞬间出现边框。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接