【问题标题】:how to put a border around an android relativelayout?如何在android relativelayout周围放置边框?
【发布时间】:2018-12-06 04:56:35
【问题描述】:

我已经看到subject 关于在 android textview 周围设置边框,我使用了它。但是现在,我想在位于相对布局中的小部件周围放置一个边框。我该怎么做?

【问题讨论】:

    标签: android border android-relativelayout


    【解决方案1】:
    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            
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_border"
                android:padding="15dp">
        ...
    </RelativeLayout>
    

    【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        虽然所有提供的答案都有效,但它们非常死板。如果您想为不同的屏幕自定义边框颜色、边框厚度怎么办。为此,您应该尝试我的解决方案。我们将按照三个步骤创建自定义 RelativeLayout,允许您为底部边框提供borderColor 和Thickness。

        1)创建一个扩展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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-04
          • 2011-03-30
          • 1970-01-01
          • 1970-01-01
          • 2012-09-16
          • 1970-01-01
          相关资源
          最近更新 更多