2012-05-03 64 views
10

我已經看到這個subject圍繞android textview放置一個邊框,我用它。但是現在,我想將邊框放置在相對佈局的小部件周圍。我該怎麼做?如何在android relativelayout周圍放置邊框?

+0

既然你沒有接受任何答案,你通過這個鏈接http://stackoverflow.com/questions/832359 9 /如何添加底部邊界在相對佈局 – surhidamatya

+0

您的問題是重複的答案,請檢查此鏈接。 http://stackoverflow.com/a/17980889/801369 – dhuma1981

回答

1

創建一個FrameLayout,獲取邊框的背景顏色以及邊框寬度的邊距或邊距,然後將該FrameLayout放置在您的RelativeLayout中。將TextView放在FrameLayout中,而不是直接放在RelativeLayout中。 poof即時邊框。

+0

我不明白,請給我一個例子嗎? – AyaAndro

8
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); 
33
    res/drawable文件夾
  1. ,創建一個新的文件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> 
  • 將您小部件的背景設置爲您剛剛創建的可繪製配置
  • 例如。如果你想要把你的邊界上的RelativeLayout:

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

    雖然所提供的所有答案的工作,他們都非常rigid.what如果您要自定義邊框顏色,爲了borderThickness不同的屏幕。爲此,您應該嘗試我的解決方案。我們將按照創建自定義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> 
    
    +0

    這是矯枉過正 –