Gravity not working in my custom textView

Created a custom text view as I wanted to set the letter spacing. It works fine. But I have some problems with Gravity. Gravity is not working on my text element. Here is my textview class code:

public class MyTextView2D extends TextView {
    private Typeface typeface;
    private float letterSpacing = 0.0f;

    public MyTextView2D(Context context) {
        this(context, null);
        isInEditMode();
    }

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

    public MyTextView2D(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attributesArray = getResources().obtainAttributes(attrs, R.styleable.MyTextView);
        letterSpacing = attributesArray.getDimension(R.styleable.MyTextView_letterSpacing, 0.0f);
        Log.d("letterSpacing", "letterSpacing "+ letterSpacing);
        String fontName = attributesArray.getString(R.styleable.MyTextView_fontName);
        if(!this.isInEditMode()) {
            if (null == fontName) {
                typeface = Fonts.getBlockBertholdRegular(context);
            } else {
                typeface = Fonts.get(context, fontName);
            }
            super.setTypeface(typeface);
        }
        this.invalidate();
    }

    public float getLetterSpacing() {
        return letterSpacing;
    }

    public void setLetterSpacing(float letterSpacing) {
        this.letterSpacing = letterSpacing;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint p = buildPaint();

        CharSequence text = getText();
        float x = getPaddingLeft();
        float[] textWidths = new float[2];
        p.setTypeface(getTypeface());
        p.setTextSize(getTextSize());
        Rect charBounds = new Rect();
        for(int i = 0; i < text.length(); i++) {
            String chr = "" + text.charAt(i);
            p.getTextWidths(chr, textWidths);
            canvas.drawText(chr, x, getTextSize(), p);
            p.getTextBounds(chr, 0, 1, charBounds);
            x += charBounds.right + getLetterSpacing();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Paint p = buildPaint();

        CharSequence text = getText();
        float x = getPaddingLeft();
        float[] textWidths = new float[2];
        p.setTypeface(getTypeface());
        p.setTextSize(getTextSize());
        Rect charBounds = new Rect();
        for(int i = 0; i < text.length(); i++) {
            String chr = "" + text.charAt(i);
            p.getTextWidths(chr, textWidths);
            p.getTextBounds(chr, 0, 1, charBounds);
            x += charBounds.right + getLetterSpacing();
        }
        setMeasuredDimension((int)x, (int)getTextSize());
    }

    private Paint buildPaint() {
        Paint p = new Paint();
        p.setColor(getTextColors().getDefaultColor());
        p.setStrokeWidth(1.5f);
        p.setAntiAlias(true);
        return p;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        invalidate();
    }
}

      

What do I do gravity? Perhaps other properties don't work either, but I'm not up to us.

EDIT

 <mypakeg.customviews.MyTextView2D
     android:id="@+id/subtotal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:paddingRight="15px"
     android:text="$0.00"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:textColor="#333333"
     android:textSize="11.75sp"
     android:textStyle="bold"
     skip:letterSpacing="1px"
     android:layout_weight="0.80"/>

      

+3


source to share





All Articles