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?
risposta
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.
Non ho capito, potresti per favore darmi un esempio? – AyaAndro
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);
- nella cartella
res/drawable
, creare un nuovo filebackground_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>
- 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>
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>
Questo è eccessivo –
Poiché non hai accettato alcuna risposta, hai seguito questo link http://stackoverflow.com/questions/832359 9/how-to-add-bottom-border-in-relativelayout – surhidamatya
La tua domanda è duplicata per la risposta si prega di controllare questo link. http://stackoverflow.com/a/17980889/801369 – dhuma1981