2012-05-03 4 views
10

Ich habe diese subject gesehen über einen Rahmen um ein Android Textview Puting, und ich benutzte es. Aber jetzt möchte ich Widgets, die sich in einem relativen Layout befinden, umranden. Wie kann ich es tun?Wie legt man einen Rand um ein androides Relay?

+0

Da Sie eine Antwort akzeptiert hvaen't, hast du über diesen Link gehen http://stackoverflow.com/questions/832359 9/how-to-Add-bottom-border-in-relativelayout – surhidamatya

+0

Ihre Anfrage zur Beantwortung ist duplizieren Sie bitte diesem Link. http://stackoverflow.com/a/17980889/801369 – dhuma1981

Antwort

1

erstellen FrameLayout, die die Hintergrundfarbe Ihrer Grenze, und eine Marge oder Polstern Ihre Rahmenbreite und Ort, dass FrameLayout in Ihrem RelativeLayout bekommt. Platzieren Sie das TextView in Ihrem FrameLayout statt direkt in dem RelativeLayout. poof sofortige Grenze.

+0

ich nicht verstand, konnte man bitte geben Sie mir ein Beispiel nennen? – 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. in Ihrem res/drawable Ordner eine neue Datei In dieser Datei background_border.xml

erstellen, werden Sie den Hintergrund für Widget wie folgt definieren:

<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. stellen Sie den Hintergrund Ihres Widgets in die ziehbar Konfiguration haben Sie gerade erstellt

zB. wenn Sie Ihre Grenze wollen auf einem relativelayout setzen:

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

Obwohl alle gegebenen Antworten arbeiten, sind sie sehr rigid.what wenn Sie Grenzfarbe anpassen möchten, BorderThickness für verschiedene Bildschirme. Dafür sollten Sie meine Lösung ausprobieren. Wir werden drei Schritte beim Erstellen eines benutzerdefinierten RelativeLayouts ausführen, mit dem Sie borderColor und Thickness für den unteren Rand bereitstellen können.

1) Erstellen Sie eine Klasse, die RelativeLayout und außer Kraft setzen auf Draw-Methode

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) definieren frisierbar Eigenschaften in attrs.xml

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

3) Sie sind fertig erstreckt, und Sie können es verwenden, wie

<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

Das ist Overkill –

Verwandte Themen