TextView gets out of RelativeLayout (with rounded edges)

0

I'm trying to make the items in my list look like in figure 2, but they only look like in figure 1. I researched a lot about it, but I found very little about it (nor do I know very well what to search).

FollowmyXMLfile:

<?xmlversion="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/lista_linhas"
    android:padding="5dp">

    <ImageView
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@drawable/border_radius"
        android:layout_margin="0dp"
        android:padding="0dp"
        android:id="@+id/teste"
        android:layout_centerInParent="true"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:padding="0dp"
        android:id="@+id/circulo">

        <TextView
            android:layout_width="5dp"
            android:layout_height="48dp"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:layout_alignParentLeft="true"
            android:id="@+id/cor_esquerda" />

        <TextView
            android:layout_width="38dp"
            android:layout_height="48dp"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:gravity="center"
            android:textAlignment="center"
            android:layout_centerInParent="true"
            android:textColor="@color/preto"
            android:id="@+id/numero" />

        <TextView
            android:layout_width="5dp"
            android:layout_height="48dp"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:layout_alignParentRight="true"
            android:id="@+id/cor_direita" />

    </RelativeLayout>

</RelativeLayout>

border-radius.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ffffff" />

    <padding
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners android:radius="50dp" />

</shape>
    
asked by anonymous 29.09.2014 / 19:53

1 answer

1

You can create a custom view:

package br.com.mion.android;

public class MyView extends View {

    private Paint mPaint;
    private Rect mRect;
    private Path mPath;

    public MyView(Context context) {
        this(context, null, 0);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mRect = new Rect();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // Define um path "ovalado" do tamanho da View.
        RectF r = new RectF();
        r.set(0, 0, (float) w, (float) h);
        mPath = new Path();
        mPath.addOval(r, Direction.CW);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // Corta o canvas com o path "ovalado".
        canvas.clipPath(mPath);
        // Pinta o fundo de vermelho.
        canvas.drawColor(Color.RED);

        // Desenha o retângulo vermelho da esquerda.
        mRect.set(0, 0, (int) (getWidth() * .25f), getHeight());
        canvas.drawRect(mRect, mPaint);

        // Desenha o retângulo vermelho da direita.
        mRect.set((int) (getWidth() * .75f), 0, getWidth(), getHeight());
        canvas.drawRect(mRect, mPaint);

    }

};

And use it in your layout file like this, for example:

<br.com.mion.android.MyView
    android:layout_width="100dp"
    android:layout_height="100dp" />
    
04.11.2014 / 23:03