2012-01-22 41 views
27

Tôi đang gặp một tình huống trong trang Đồ thị nơi LinearLayout sẽ hiển thị TextView với 90 độ xoay.Cách Xoay TextView 90 Độ và hiển thị

+0

http://stackoverflow.com/questions/1258275/vertical-rotated-label-in-android kiểm tra điều này và giải quyết vấn đề của tôi – sakshi

+0

Có thể thực hiện điều này trong XML theo API 11 (Android 3.0). http://stackoverflow.com/questions/3774770/sideways-view-with-xml-android – chobok

Trả lời

35

Cách nhanh nhất và thuận tiện nhất là để Rotate bởi Animation

hoạt hình sử dụng xoay trên TextView thường xuyên của bạn như vậy.

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 Code:

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

Điều này là tốt nhưng không giúp tôi rất nhiều kiểm tra này http://stackoverflow.com/questions/1258275/vertical-rotated-label -in-android đã giúp tôi rất nhiều trong cse của tôi – sakshi

12

Trong android cho bất kỳ quan điểm mới có một phương pháp gọi là setRotation(float) bạn có thể sử dụng nó

textview.setRotation(float); 

nhưng xin lưu ý rằng phương pháp này được thêm vào trong cấp API 11

vì vậy nếu bạn muốn hỗ trợ nó, bạn có thể sử dụng

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

Một chút giải thích sẽ là tốt đẹp. –

+0

Tôi đã chỉnh sửa câu trả lời để biết thêm thông tin –

39

Hãy thử điều này ::

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

FYI. Nhu cầu của nó Build.VERSION.SDK_INT> 11 – MilapTank

+2

vấn đề trong cách tiếp cận này là kích thước chiều rộng không thay đổi. Bất kỳ ý tưởng làm thế nào để vượt qua điều này? – abh22ishek

3

[SOLVED] Sau một năm hạnh phúc này vào danh sách xô tôi mà không có bất kỳ phù hợp câu trả lời trên các diễn đàn tôi cuối cùng đã sắp xếp này!

Bí quyết ở đây là mã hóa cứng layout_width và layout_height của số TextView lớn hơn văn bản sẽ bao giờ - ví dụ: 100dp x 100dp.

Sau đó, đặt TextView trong một FrameLayout và rẽ cắt ra cho FrameLayoutandroid:clipChildren="false" và clip để đệm tắt cho TextView android:clipToPadding="false":

TextView with hard-coded height and width

Các TextView bây giờ sẽ nổi trong FrameLayout. Sử dụng các cài đặt trọng lực TextView để di chuyển Văn bản trong phạm vi ranh giới của nó.

Sau đó sử dụng các thiết lập layout_gravity để di chuyển các đường biên trong phụ huynh FrameLayout bên trong nó:

Dưới đây là một ví dụ về quyền và dưới liên kết văn bản xoay qua -90 độ:

Solution example

[UPDATE] Ví dụ về XML cho khung nhìn xoay khung văn bản nhà ở:

 <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

Vui lòng chia sẻ mã xml của bạn để chúng tôi có thể nhanh chóng hiểu được giải thích của bạn về câu trả lời của bạn. – ProgDevCode

+0

@ProgDevCode đã thêm một số XML theo yêu cầu. – 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

Cảm ơn ........... –

0

Nếu bạn không cần hoạt ảnh thì bạn có thể thử giải pháp được cung cấp trong một question tương tự. Một văn bản sẽ được viết từ dưới lên trên.

Nó hỗ trợ tất cả các tính năng TextView cơ bản:

  • kích thước văn bản
  • màu chữ
  • font chữ
  • văn bản đệm
  • sử dụng qua XML

điểm chính là để ghi đè onDraw()onMeasure() phương pháp dựa trên thông số văn bản của bạn:

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

Vui lòng xem Vertical (rotated) label in Android để xem mã nguồn đầy đủ.

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