2012-01-22 12 views
27

प्रदर्शित करें मुझे ग्राफ पृष्ठ में स्थिति है जहां LinearLayout को TextView 90 डिग्री घूर्णन के साथ प्रदर्शित करना चाहिए।टेक्स्टव्यू 90 डिग्री कैसे घुमाएं और

+0

http://stackoverflow.com/questions/1258275/vertical-rotated-label-in-android ने इसे चेक किया और मेरी समस्या हल की – sakshi

+0

एपीआई 11 (एंड्रॉइड 3.0) के रूप में एक्सएमएल में ऐसा करना संभव है। http://stackoverflow.com/questions/3774770/sideways-view-with-xml-android – chobok

उत्तर

35

सबसे तेज और सबसे सुविधाजनक तरीका है तो जैसे अपने नियमित रूप से TextView पर Animation

उपयोग घुमाने एनीमेशन द्वारा Rotate है।

rotateAnimation.xml:

<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
      android:fromDegrees="0" 
      android:toDegrees="-90" 
      android:pivotX="50%" 
      android:duration="0" 
      android:fillAfter="true" /> 

जावा कोड:

TextView text = (TextView)findViewById(R.id.txtview);  
    text.setText("rotated text here"); 

    RotateAnimation rotate= (RotateAnimation)AnimationUtils.loadAnimation(this,R.anim.rotateAnimation); 
    text.setAnimation(rotate); 
+0

यह अच्छा है लेकिन मुझे यह बहुत मदद नहीं मिली है http://stackoverflow.com/questions/1258275/vertical-rotated- लेबल -इन-एंड्रॉइड ने मुझे अपने सीएस – sakshi

12

किसी भी नए दृश्य के लिए एंड्रॉयड में एक विधि कहा जाता setRotation(float) है आप इसे

textview.setRotation(float); 

लेकिन उपयोग कर सकते हैं तो कृपया ध्यान दें कि यह विधि API स्तर 11

में जोड़ा गया है

ताकि आप इसे समर्थन करना चाहते हैं, तो आप उपयोग कर सकते इस

if (Build.VERSION.SDK_INT < 11) { 

    RotateAnimation animation = new RotateAnimation(oldAngel, newAngel); 
    animation.setDuration(100); 
    animation.setFillAfter(true); 
    watermarkText.startAnimation(animation); 
} else { 

    watermarkText.setRotation(progress); 
} 
+1

में बहुत मदद की है थोड़ा और स्पष्टीकरण अच्छा होगा। –

+0

मैंने अधिक जानकारी –

39

इस :: प्रयास करें

<TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:rotation="-95" 
       android:text="2" 
       /> 
+1

FYI के लिए उत्तर संपादित किया। इसकी आवश्यकता Build.VERSION.SDK_INT> 11 – MilapTank

+2

इस दृष्टिकोण में समस्या चौड़ाई आकार बदलती नहीं है। कोई विचार यह कैसे दूर करने के लिए? – abh22ishek

3

[हल] किसी भी उपयुक्त बिना मेरी बाल्टी सूची पर इस किया जा रहा है के एक साल बाद मंचों पर जवाब मैंने आखिरकार इसे हल किया है!

यह चाल यहां लेआउट_विड्थ और लेआउट_हेइट को TextView टेक्स्ट से बड़ा होने के लिए हार्ड-कोड करना है - उदा। 100 डीपी एक्स 100 डीपी।

फिर एक FrameLayout में TextView जगह है और FrameLayoutandroid:clipChildren="false" के लिए बंद कर देते हैं और कतरन TextView android:clipToPadding="false" के लिए बंद करने के लिए padding क्लिप:

TextView with hard-coded height and width

TextView अब FrameLayout में तैर सकती है। टेक्स्ट को इसकी सीमा के भीतर ले जाने के लिए TextView गुरुत्वाकर्षण सेटिंग्स का उपयोग करें।

तो यह भीतर माता पिता FrameLayout भीतर सीमा स्थानांतरित करने के लिए layout_gravity सेटिंग्स का उपयोग करें:

Solution example

:

यहाँ सही और नीचे गठबंधन पाठ का एक उदाहरण के माध्यम से -90 डिग्री घुमाया है [अद्यतन] फ्रेम दृश्य आवास घूर्णन पाठ के लिए उदाहरण एक्सएमएल:

 <FrameLayout 
      android:layout_width="@dimen/item_col_width_val" 
      android:layout_height="match_parent" 
      android:layout_weight="0.25" 
      android:padding="@dimen/table_header_margin"> 
      <TextView 
       android:layout_width="@dimen/table_title_row_height" 
       android:layout_height="@dimen/table_title_row_height" 
       android:layout_gravity="bottom|end" 
       android:gravity="bottom" 
       android:rotation="-90" 
       android:text="@string/asm_col_title_in_stock" 
       android:textColor="@color/colorGood" /> 
     </FrameLayout> 
+0

कृपया अपना एक्सएमएल कोड साझा करें ताकि हम आपके उत्तर की व्याख्या को और अधिक तेज़ी से समझ सकें। – ProgDevCode

+0

@ProgDevCode ने अनुरोध के रूप में कुछ एक्सएमएल जोड़ा। – user7653815

4
<TextView 
      android:id="@+id/rotated_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:alpha=".3" 
      android:background="@drawable/grey_capsule" 
      android:gravity="center" 
      android:textColor="@android:color/white" 
      android:textSize="14sp" 
      android:padding="4dp" 
      android:layout_marginLeft="-50dp" 
      android:rotation="-90" 
      android:text="Andrew Coder" /> 
+0

धन्यवाद ........... –

0

यदि आपको एनीमेशन की आवश्यकता नहीं है तो आप एक समान question में दिए गए समाधान का प्रयास कर सकते हैं। एक पाठ नीचे से ऊपर तक लिखा जाएगा।

  • पाठ का आकार
  • पाठ रंग
  • पाठ फ़ॉन्ट
  • पाठ गद्दी
  • XML के माध्यम से उपयोग

मुख्य बात करने के लिए है:

यह सभी बुनियादी TextView सुविधाओं का समर्थन करता ओवरराइड onDraw() और,210 तरीकों अपने पाठ का मापदंडों के आधार पर:

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

     if (!this._text.isEmpty()) 
     { 
      float textHorizontallyCenteredOriginX = this._measuredHeight/2f; 
      float textHorizontallyCenteredOriginY = this._ascent; 

      canvas.translate(textHorizontallyCenteredOriginY, textHorizontallyCenteredOriginX); 
      canvas.rotate(-90); 
      canvas.drawText(this._text, 0, 0, this._textPaint); 
     } 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     try 
     { 
      this._textPaint.getTextBounds(this._text, 0, this._text.length(), this._textBounds); 

      this._tempView = new TextView(getContext()); 
      this._tempView.setPadding(this._leftPadding, this._topPadding, this._rightPadding, this._bottomPadding); 
      this._tempView.setText(this._text); 
      this._tempView.setTextSize(TypedValue.COMPLEX_UNIT_PX, this._textSize); 
      this._tempView.setTypeface(this._typeface); 

      this._tempView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

      this._measuredWidth = this._tempView.getMeasuredHeight(); 
      this._measuredHeight = this._tempView.getMeasuredWidth(); 

      this._ascent = this._textBounds.height()/2 + this._measuredWidth/2; 

      setMeasuredDimension(this._measuredWidth, this._measuredHeight); 
     } 
     catch (Exception e) 
     { 
      setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
      Log.e(LOG_TAG, Log.getStackTraceString(e)); 
     } 
    } 

कृपया, पूर्ण स्रोत कोड को देखने के लिए Vertical (rotated) label in Android पर एक नज़र डालें।

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