2012-05-03 5 views
10

Ho visto questo subject di mettere un bordo attorno a una vista testo Android, e l'ho usato. Ma ora, vorrei mettere un confine attorno ai widget che sono collocati in un layout relativo. Come posso farlo?come mettere un confine attorno a un relativelayout Android?

+0

Poiché non hai accettato alcuna risposta, hai seguito questo link http://stackoverflow.com/questions/832359 9/how-to-add-bottom-border-in-relativelayout – surhidamatya

+0

La tua domanda è duplicata per la risposta si prega di controllare questo link. http://stackoverflow.com/a/17980889/801369 – dhuma1981

risposta

1

Creare un FrameLayout che ottiene il colore di sfondo del bordo e un margine o spaziatura della larghezza del bordo e posizionare il FrameLayout in RelativeLayout. Posiziona TextView nel tuo FrameLayout invece che direttamente in RelativeLayout. poof bordo istantaneo.

+0

Non ho capito, potresti per favore darmi un esempio? – 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. nella cartella res/drawable, creare un nuovo file background_border.xml

In questo file, verrà definito lo sfondo per widget di simile a questo:

<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. imposta lo sfondo del tuo widget sulla configurazione estraibile che hai appena creato

es. se si vuole mettere il bordo su relativelayout:

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

Anche se tutte le risposte fornite lavoro, sono molto rigid.what se si desidera personalizzare il colore del bordo, BorderThickness per schermi differenti. per questo dovresti provare la mia soluzione. Stiamo andando a seguire tre passaggi nella creazione di un RelativeLayout personalizzato che ti consente di fornire borderColor e Thickness per il bordo inferiore.

1) Creare una classe che estende RelativeLayout e sovrascrivere il metodo 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) Definire le proprietà stylable in attrs.xml

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

3) si è fatto e si può usare come

<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

Questo è eccessivo –