Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're already overriding <code>onDraw</code>, why not just draw the text strings yourself? Rather than go through the overhead of adding TextViews and messing with the padding, just use <code>canvas.drawText</code> to physically draw the text strings in the right place.</p> <p>You can specify the style and color of the text using a <code>Paint</code> object:</p> <pre><code>Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(r.getColor(R.color.text_color)); textPaint.setFakeBoldText(true); textPaint.setSubpixelText(true); textPaint.setTextAlign(Align.LEFT); </code></pre> <p>And get the exact positioning by using the measureText method on that Paint object to find what width a particular string would be when drawn on a canvas:</p> <pre><code>textWidth = (int)textPaint.measureText(mTexts[i]); </code></pre> <p>Then you can iterate over your array of text strings and draw each string in the right place.</p> <pre><code>@Override protected void onDraw(Canvas canvas) { int myWidth = getMeasuredWidth()-LEFT_PADDING-RIGHT_PADDING; int separation = myWidth / (mSize-1); for (int i = 0; i++; i &lt; mSize) { int textWidth = (int)textPaint.measureText(mTexts[i]); canvas.drawText(mTexts[i], LEFT_PADDING+(i*separation)-(int)(textWidth/2), TOP_PADDING, textPaint); } } </code></pre> <p>You'll probably want to do the measurements in onMeasure instead of onDraw, and you should probably only measure the width of each string when you change the text or the paint, but I've put it all in one place to (hopefully) make it easier to follow.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload