2012-01-22 233 views
27

我在Graph页面中有一种情况,LinearLayout应该显示TextView旋转90度。如何旋转TextView 90度并显示

+0

http://stackoverflow.com/questions/1258275/vertical-rotated-label-in-android检查了这个并解决了我的问题 – sakshi

+0

从API 11(Android 3.0)开始,可以在XML中执行此操作。 http://stackoverflow.com/questions/3774770/sideways-view-with-xml-android – chobok

回答

35

最快,最方便的方法是通过Rotate您的定期的TextView像这样Animation

使用旋转动画。

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" /> 

Java代码:

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-label -in-android在我的cse中帮了我很多 – sakshi

12

Android中的任何新观点有一个叫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

供参考。它需要Build.VERSION.SDK_INT> 11 – MilapTank

+2

这个方法的问题是宽度大小没有改变。任何想法如何克服这一点? – abh22ishek

3

[解决]一年这是我遗愿清单上没有任何合适的后我终于在论坛上回答了这个问题!

这里的技巧是硬编码TextView的layout_width和layout_height大于文本的大小 - 例如, 100dp x 100dp。

然后将其放置在一个FrameLayoutTextView,并打开关断限幅为FrameLayoutandroid:clipChildren="false"和夹到填充关闭TextView的android:clipToPadding="false"

TextView with hard-coded height and width

TextView现在将漂浮在FrameLayout。使用重力设置来移动边界内的文本。

然后使用layout_gravity设置在其内的父FrameLayout内移动边界:

这里是通过-90度旋转的右侧和底部对齐的文本的例子:

Solution example

[UPDATE]示例XML用于帧视图壳体旋转文本:

 <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

请分享您的XML代码,以便我们更快地理解您对答案的解释。 – ProgDevCode

+0

@ProgDevCode根据请求添加了一些XML。 – 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中提供的解决方案。文字将自下而上写入。

它支持所有基本的TextView特点:

  • 文字大小
  • 文本颜色
  • 文本字体
  • 文本填充
  • 通过XML的使用

要点是覆写onDraw()onMeasure()种方法基于文本的参数:

@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看到完整的源代码。