2016-03-30 34 views
5

Cách tạo TextView dọc (xoay 90 ° hoặc -90 °) với hỗ trợ hình elip (có nghĩa là văn bản bị cắt bớt nếu không có đủ chỗ trong chế độ xem và "..." được thêm vào để cho biết cắt ngắn), có thể được định kích thước với dp -values, wrap_contentmatch_parent?TextView dọc với Ellipsize và tất cả các tính năng TextView mặc định khác hỗ trợ

Tất cả các tùy chọn TextView khác cũng phải được tôn trọng.

Nó cũng không cần thêm bất kỳ mã nào để thực thi phép quay hoặc loại khác và phải hiển thị chính xác trong giao diện người dùng.

This solution hoạt động tuyệt vời, tuy nhiên, ghi đè onDraw() kết quả trong hình elip và các tính năng khác không hoạt động nữa. Văn bản sẽ biến mất đơn giản nếu nó quá lớn để hiển thị, hoặc những điều kỳ lạ khác sẽ xảy ra.

Sau đây là xoay vòng ok, nhưng phép tính elip được tính toán dựa trên chiều rộng của khung nhìn, nó phải dựa trên chiều cao nếu được quay 90 °.

protected void onDraw(Canvas canvas) { 
    canvas.save(); 

    canvas.rotate(90, 0, getPaint().getTextSize() +5); 
    super.onDraw(canvas); 
    canvas.restore(); 
} 

android:rotation trên TextView không có giải pháp tốt vì định vị và kích thước không hoạt động bình thường.

Tùy chọn, Chế độ xem sẽ cho phép văn bản được xoay bất kỳ góc nào.

+0

đã thử với một hình ảnh động ngay lập tức như đã giải thích ở đây: http://stackoverflow.com/a/8959448/775894? –

+0

Tôi đã làm rõ ở trên. Không có mã bổ sung nào ngoài kiểm soát nên được sử dụng, nó cũng phải được hiển thị chính xác trong UI-Editor. – Marc

Trả lời

0

bạn có thể sử dụng một số thứ như thế này có thể là điều này sẽ giúp bạn.

public class VerticalLabelView extends View { 
    private TextPaint mTextPaint; 
    private String mText; 
    private int mAscent; 
    private Rect text_bounds = new Rect(); 

    final static int DEFAULT_TEXT_SIZE = 15; 

    public VerticalLabelView(Context context) { 
     super(context); 
     initLabelView(); 
    } 

    public VerticalLabelView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initLabelView(); 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalLabelView); 

     CharSequence s = a.getString(R.styleable.VerticalLabelView_text); 
     if (s != null) setText(s.toString()); 

     setTextColor(a.getColor(R.styleable.VerticalLabelView_textColor, 0xFF000000)); 

     int textSize = a.getDimensionPixelOffset(R.styleable.VerticalLabelView_textSize, 0); 
     if (textSize > 0) setTextSize(textSize); 

     a.recycle(); 
    } 

    private final void initLabelView() { 
     mTextPaint = new TextPaint(); 
     mTextPaint.setAntiAlias(true); 
     mTextPaint.setTextSize(DEFAULT_TEXT_SIZE); 
     mTextPaint.setColor(0xFF000000); 
     mTextPaint.setTextAlign(Align.CENTER); 
     setPadding(3, 3, 3, 3); 
    } 

    public void setText(String text) { 
     mText = text; 
     requestLayout(); 
     invalidate(); 
    } 

    public void setTextSize(int size) { 
     mTextPaint.setTextSize(size); 
     requestLayout(); 
     invalidate(); 
    } 

    public void setTextColor(int color) { 
     mTextPaint.setColor(color); 
     invalidate(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     mTextPaint.getTextBounds(mText, 0, mText.length(), text_bounds); 
     setMeasuredDimension(
       measureWidth(widthMeasureSpec), 
       measureHeight(heightMeasureSpec)); 
    } 

    private int measureWidth(int measureSpec) { 
     int result = 0; 
     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 = text_bounds.height() + getPaddingLeft() + getPaddingRight(); 

      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 int measureHeight(int measureSpec) { 
     int result = 0; 
     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 
      result = text_bounds.width() + getPaddingTop() + getPaddingBottom(); 

      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; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     float text_horizontally_centered_origin_x = getPaddingLeft() + text_bounds.width()/2f; 
     float text_horizontally_centered_origin_y = getPaddingTop() - mAscent; 

     canvas.translate(text_horizontally_centered_origin_y, text_horizontally_centered_origin_x); 
     canvas.rotate(-90); 
     canvas.drawText(mText, 0, 0, mTextPaint); 
    } 
}

Và trong attrs.xml: bạn

<resources> 
    <declare-styleable name="VerticalLabelView"> 
     <attr name="text" format="string" /> 
     <attr name="textColor" format="color" /> 
     <attr name="textSize" format="dimension" /> 
    </declare-styleable> 
</resources> 
+2

đây là bản sao & dán từ đây: http://stackoverflow.com/a/2599518/5858875. Nó không hỗ trợ hình elip. – Marc

Các vấn đề liên quan