How To Handle Difference Screen Sizes So That A Textview Scales
I have to distribute text over a view uniformly. Have been unable to figure out a way that satisfies all screen sizes or even the common screen sizes. The layout I want to achieve
Solution 1:
Use a custom View and override onDraw to draw the numbers. The layout containing the View can specify its size using dimensions (from res/values/), and the View will automatically work out what font size and spacing between the numbers based on those dimensions.
E.g.
Custom View:
publicclassColumnNumbersViewextendsView
{
privatefinalstaticintNUMBER_OF_COLUMNS=25;
privatefloat textSize;
privatefinalPainttextPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
...
Getting the size:
@OverrideprotectedvoidonLayout(...
{
super.onLayout(...
// work out our text size from getHeight()
textSize = getHeight()/2; // or something like that// work out the spacing between the numbers along the x axis
textPosXInc = (getWidth() / NUMBER_OF_COLUMNS) / 2; // or something like that
}
Doing the drawing:
@OverrideprotectedvoidonDraw(final Canvas canvas)
{
super.onDraw(canvas);
intx=0;
for(int i=0; i < NUMBER_OF_COLUMNS; i++)
{
finalStringnumber= String.valueOf(i);
finalinthalfWidth= textPaint.measureText(number) / 2;
canvas.drawText(number, x - halfWidth, 0, textPaint);
x += textPosXInc;
}
}
That should draw something close, the first and last numbers won't draw correctly though, I'll leave that for you to fix.
EDIT
Keep in mind that you can't use wrap_content for this View's dimensions, because it doesn't specify a size to wrap by overriding onMeasure(). So it'll require specific sizes set, or match_parent.
Post a Comment for "How To Handle Difference Screen Sizes So That A Textview Scales"