TextView에 Rotation주기 (글자를 세로로 표시)
조회수 6644회
텍스트를 세로로 표시해주기위해서 rotation에 -90.0을 주었는데 여기서 문제가 생겨 질문드려요 ㅠㅠ
지금 이 상태에서 -90.0으로 주었는데
저렇게 돌아가서 보이는 것을 구현하려는 것은 맞지만, 저 택스트 뷰를 감싸고 있는 LinearLayout의 크기가 바뀌지 않네요 ㅠ
1번 사진에서 LinearLayout의 width를 wrap_content로 두었더니, rotation되기 전의 크기로 고정이 되어서 회전을 한 후에도 그 크기로 남아서 사진2처럼 텍스트뷰 양옆으로 공간이 생겨있습니다. 저것을 원하지는 않았는데 말이죠 ㅠ 그래서 LinearLayout의 width를 텍스트 사이즈와 같게 해봤더니, 로테이션 되기전에 텍스트뷰안의 텍스트가 두줄로 써진 상태로 로테이션 되어 회전후에도 두줄로 남아버렸습니다.
사진2번에서 텍스트뷰의 양옆 공백을 없에려면 어떻게 해야하나요?
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
TextView를 상속받아서 뷰를 돌리면 될 것 같네요.
package com.example.myapplication; import android.content.Context; import android.graphics.Canvas; import android.text.TextPaint; import android.util.AttributeSet; import android.widget.TextView; public class CustomTextView extends TextView { public CustomTextView(Context context, AttributeSet attrs){ super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(heightMeasureSpec, widthMeasureSpec); setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); } @Override protected void onDraw(Canvas canvas){ TextPaint textPaint = getPaint(); textPaint.setColor(getCurrentTextColor()); textPaint.drawableState = getDrawableState(); canvas.save(); canvas.translate(getWidth(), 0); canvas.rotate(90); //원하는 방향 설정 canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop()); getLayout().draw(canvas); canvas.restore(); } }
그리고 xml에
<com.example.myapplication.CustomTextView android:id="@+id/txt_123" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="abcdefgdddddd" />
이렇게 지정해주시면 됩니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력