2012-05-03 37 views
10

Tôi đã nhìn thấy điều này subject về cách đặt đường viền xung quanh chế độ xem văn bản Android và tôi đã sử dụng nó. Nhưng bây giờ, tôi muốn đặt một biên giới xung quanh các vật dụng được đặt vào một bố trí tương đối. Tôi làm nó như thế nào?cách đặt đường viền xung quanh android relativelayout?

+0

Vì bạn không chấp nhận bất kỳ câu trả lời nào, bạn đã đi qua liên kết này http://stackoverflow.com/questions/832359 9/how-to-add-bottom-border-in-relativelayout – surhidamatya

+0

Câu hỏi của bạn trùng lặp với câu trả lời, vui lòng kiểm tra liên kết này. http://stackoverflow.com/a/17980889/801369 – dhuma1981

Trả lời

1

Tạo FrameLayout có màu nền của đường viền của bạn và lề hoặc đệm của chiều rộng đường viền của bạn và đặt FrameLayout đó trong RelativeLayout của bạn. Đặt TextView trong FrameLayout của bạn thay vì trực tiếp trong RelativeLayout. poof đường viền tức thì.

+0

Tôi không hiểu, bạn có thể cho tôi một ví dụ không? – 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
  1. trong thư mục res/drawable của bạn, tạo ra một tập tin mới background_border.xml

Trong tập tin này, bạn sẽ xác định nền cho widget như thế này:

<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. đặt nền của tiện ích con thành cấu hình có thể vẽ được mà bạn vừa tạo

ví dụ: nếu bạn muốn đặt biên giới của bạn trên một RelativeLayout:

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

Mặc dù tất cả các câu trả lời cung cấp làm việc, họ rất rigid.what nếu bạn muốn tùy chỉnh màu đường viền, borderthickness cho các màn hình khác nhau. cho rằng bạn nên thử giải pháp của tôi.Chúng tôi sẽ làm theo ba bước trong việc tạo ra một RelativeLayout tùy chỉnh cho phép bạn cung cấp borderColor và độ dày cho đường viền dưới.

1) Tạo một lớp mà kéo dài RelativeLayout và ghi đè vào phương thức 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) Xác định tính chất stylable trong attrs.xml

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

3) bạn đang thực hiện và bạn có thể sử dụng nó như

<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

Đây là quá mức cần thiết –

Các vấn đề liên quan