FileDocCategorySizeDatePackage
LabelView.javaAPI DocAndroid 5.1 API5855Thu Mar 12 22:22:12 GMT 2015android.widget

LabelView

public class LabelView extends android.view.View
Example of how to write a custom subclass of View. LabelView is used to draw simple text views. Note that it does not handle styled text or right-to-left writing systems.

Fields Summary
private android.graphics.Paint
mTextPaint
private String
mText
private int
mAscent
Constructors Summary
public LabelView(android.content.Context context)
Constructor. This version is only needed if you will be instantiating the object manually (not from a layout XML file).

param
context the application environment

        super(context);
        initLabelView();
    
Methods Summary
private voidinitLabelView()
Construct object, initializing with any attributes we understand from a layout file. These attributes are defined in SDK/assets/res/any/classes.xml.

see
android.view.View#View(android.content.Context, android.util.AttributeSet) public LabelView(Context context, AttributeSet attrs) { super(context, attrs); initLabelView(); Resources.StyledAttributes a = context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequence s = a.getString(R.styleable.LabelView_text); if (s != null) { setText(s.toString()); } ColorStateList textColor = a.getColorList(R.styleable. LabelView_textColor); if (textColor != null) { setTextColor(textColor.getDefaultColor(0)); } int textSize = a.getInt(R.styleable.LabelView_textSize, 0); if (textSize > 0) { setTextSize(textSize); } a.recycle(); }

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(16);
        mTextPaint.setColor(0xFF000000);

        mPaddingLeft = 3;
        mPaddingTop = 3;
        mPaddingRight = 3;
        mPaddingBottom = 3;
    
private intmeasureHeight(int measureSpec)
Determines the height of this view

param
measureSpec A measureSpec packed into an int
return
The height of the view, honoring constraints from measureSpec

        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = (int) (-mAscent + mTextPaint.descent()) + mPaddingTop
                    + mPaddingBottom;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    
private intmeasureWidth(int measureSpec)
Determines the width of this view

param
measureSpec A measureSpec packed into an int
return
The width of the view, honoring constraints from measureSpec

        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = (int) mTextPaint.measureText(mText) + mPaddingLeft
                    + mPaddingRight;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    
protected voidonDraw(android.graphics.Canvas canvas)
Render the text

see
android.view.View#onDraw(android.graphics.Canvas)

        super.onDraw(canvas);
        canvas.drawText(mText, mPaddingLeft, mPaddingTop - mAscent, mTextPaint);
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

see
android.view.View#measure(int, int)

        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    
public voidsetText(java.lang.String text)
Sets the text to display in this label

param
text The text to display. This will be drawn as one line.

        mText = text;
        requestLayout();
        invalidate();
    
public voidsetTextColor(int color)
Sets the text color for this label

param
color ARGB value for the text

        mTextPaint.setColor(color);
        invalidate();
    
public voidsetTextSize(int size)
Sets the text size for this label

param
size Font size

        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();