2016-03-30 4 views
5

समर्थन लंबवत टेक्स्ट व्यू (90 डिग्री या -90 डिग्री से घूर्णन) को इलिप्सिज़ समर्थन के साथ कैसे बनाएं (जिसका अर्थ यह है कि यदि टेक्स्ट में पर्याप्त कमरा नहीं है तो पाठ को छोटा कर दिया गया है , और "..." को छंटनी को इंगित करने के लिए जोड़ा गया है), जिसे dp -values, wrap_content और match_parent के साथ आकार दिया जा सकता है?इलिप्सिज़ के साथ वर्टिकल टेक्स्ट व्यू और अन्य सभी डिफॉल्ट टेक्स्ट व्यू फीचर्स

अन्य सभी टेक्स्ट व्यू विकल्पों को भी सम्मानित किया जाना चाहिए।

इसे घूर्णन निष्पादित करने के लिए किसी भी अतिरिक्त कोड की आवश्यकता नहीं है, और यूआई-संपादक में सही ढंग से प्रदर्शित होना चाहिए।

This solution महान काम करता है, हालांकि, onDraw() ओवरराइडिंग में परिणाम इलिप्सिज़ और अन्य सुविधाएं अब काम नहीं कर रही हैं। यदि टेक्स्ट प्रदर्शित करने के लिए बहुत बड़ा है, तो टेक्स्ट बस गायब हो जाएगा, या अन्य अजीब चीजें घटित होंगी।

निम्नलिखित रोटेशन ठीक है, लेकिन इलिप्सिज़ की गणना चौड़ाई के आधार पर की जाती है, यह 9 0 डिग्री बदलकर ऊंचाई पर आधारित होना चाहिए। स्थिति निर्धारण और आकारण ठीक से काम नहीं करता है के रूप में

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

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

android:rotation TextView पर, कोई अच्छा समाधान है।

एक विकल्प के रूप में, दृश्य को किसी भी कोण को घुमाने के लिए पाठ की अनुमति देनी चाहिए।

+0

था: http://stackoverflow.com/a/8959448/775894? –

+0

मैंने ऊपर स्पष्ट किया। नियंत्रण के बाहर कोई अतिरिक्त कोड इस्तेमाल नहीं किया जाना चाहिए, इसे यूआई-एडिटर में भी सही ढंग से प्रदर्शित किया जाना चाहिए। – Marc

उत्तर

0

आप इस तरह की कुछ चीज़ों का उपयोग कर सकते हैं यह आपकी मदद करेगा।

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

और attrs.xml में: आप एक त्वरित एनीमेशन के साथ की कोशिश की तरह यहाँ समझाया

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

यह यहां से कॉपी और पेस्ट है: http://stackoverflow.com/a/2599518/5858875। यह इलिप्सिज़ का समर्थन नहीं करता है। – Marc

संबंधित मुद्दे