2012-05-03 11 views
10

He visto esto subject acerca de poner un borde alrededor de una vista de texto de Android, y lo he usado. Pero ahora, me gustaría poner un borde alrededor de los widgets que están situados en un diseño relativo. ¿Cómo puedo hacerlo?cómo poner un borde alrededor de una versión relativa de Android?

+0

Como no has aceptado ninguna respuesta, has visitado este enlace http://stackoverflow.com/questions/832359 9/how-to-add-bottom-border-in-relativelayout – surhidamatya

+0

Su pregunta está duplicada para la respuesta revise este enlace. http://stackoverflow.com/a/17980889/801369 – dhuma1981

Respuesta

1

Cree un FrameLayout que obtenga el color de fondo de su borde, y un margen o relleno del ancho de su borde, y coloque ese FrameLayout en su RelativeLayout. Coloque el TextView en su FrameLayout en lugar de directamente en RelativeLayout. poof borde inmediato.

+0

No entendí, ¿podrías darme un ejemplo? – 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. en su carpeta res/drawable, crear un nuevo archivo background_border.xml

En este archivo, definirá el fondo de widget de la siguiente manera:

<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. establece el fondo de tu widget a la configuración dibujable que acabas de crear

por ejemplo. si usted quiere poner su frontera en un RelativeLayout:

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

Aunque todas las respuestas proporcionadas funcionan, son muy rigid.what si desea personalizar el color del borde, BorderThickness para diferentes pantallas. para eso debes probar mi solución. Vamos a seguir tres pasos para crear un RelativeLayout personalizado que te permita proporcionar borderColor y Thickness para el borde inferior.

1) Crear una clase que se extiende RelativeLayout y anular en el método 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) Definición de las propiedades con estilos en attrs.xml

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

3) haya terminado y se puede utilizar como

<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

Esto es exagerado –

Cuestiones relacionadas