/** Resize text after measuring */
 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
   if (changed || mNeedsResize) {
     int widthLimit = (right - left) - getCompoundPaddingLeft() - getCompoundPaddingRight();
     int heightLimit = (bottom - top) - getCompoundPaddingBottom() - getCompoundPaddingTop();
     resizeText(widthLimit, heightLimit);
   }
   super.onLayout(changed, left, top, right, bottom);
 }
コード例 #2
0
  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    Paint textPaint = getPaint();
    String text = getText().toString();
    textPaint.getTextBounds(text, 0, text.length(), bounds);

    int textWidth = bounds.width();
    int contentWidth = drawableWidth + iconPadding + textWidth;

    int contentLeft = (int) ((getWidth() / 2.0) - (contentWidth / 2.0));
    setCompoundDrawablePadding(-contentLeft + iconPadding);
    switch (drawablePosition) {
      case LEFT:
        setPadding(contentLeft, 0, 0, 0);
        break;
      case RIGHT:
        setPadding(0, 0, contentLeft, 0);
        break;
      default:
        setPadding(0, 0, 0, 0);
    }
  }