본문 바로가기
안드로이드

안드로이드 - 텍스트뷰(TextView) 문자 정렬

by Dokon Jang 2015. 5. 12.
반응형

안드로이드 앱을 개발 할 때 TextView를 많이 사용하게 되고, TextView의 문자를 정렬하게 됩니다.

TextView의 문자를 정렬하는 방법에 대해서 알아보겠습니다.

 

※ 주의 : 

- TextView내의 문자를 정렬하기 위해서는 layout_width, layout_height의 속성이 "wrap_content"이면 정렬 할 수 없습니다.

1
2
3
4
5
6
7
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"/>
cs

 

- 아래의 이미지에서처럼 TextView내에서 정렬할 공간이 없으므로 문자를 정렬할 수 없습니다.

 

 

1. TextView 기본 문자 정렬

 - TextView의 정렬속성인 gravity를 지정하지 않으면 left, top으로 기본 문자 정렬됩니다.

1
2
3
4
5
6
7
8
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"/>
cs

 

 - 문자가 외쪽 상단에 정렬되어있습니다.

 

 

2. 문자 가로 정렬

(1) 중간 정렬 : gravity 속성 값은 center_horizontal 입니다.

1
2
3
4
5
6
7
8
9
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"
    android:gravity="center_horizontal" />
cs

 

 

(2) 우측 정렬 gravity 속성 값은 right 입니다.

1
2
3
4
5
6
7
8
9
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"
    android:gravity="right" />
cs

 

 

 

3. 문자 세로 정렬

(1) 중간 정렬 : gravity 속성 값은 center_vertical 입니다.

1
2
3
4
5
6
7
8
9
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"
    android:gravity="center_vertical" />
cs

 

 

 

(2) 바닥 정렬 : gravity 속성 값은 bottom 입니다.

1
2
3
4
5
6
7
8
9
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"
    android:gravity="bottom" />
cs

 

 

 

3. 문자 가로 + 세로 정렬

 - TextView의 문자 정렬은 가로 정렬과 세로 정렬을 함께 사용할 수 있습니다.

 - 가로는 우측정렬, 세로는 중앙정렬 : gravity 속성 값은 center_vertical right 입니다

1
2
3
4
5
6
7
8
9
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="텍스트뷰 정렬"
    android:textColor="#ff000000"
    android:background="#ffff0000"
    android:textSize="24sp"
    android:gravity="center_vertical|right" />
cs

 

 

 

태풍 - Google Play 앱

북태평양 남서부에서 발생하여 아시아 대륙 동부로 불어오는 맹렬한 열대성 저기압인 태풍에 대한 정보를 보여줍니다. 1. 태풍 경로 - 태풍경로를 보여줍니다. 2. 태풍이란 - 태풍이 무엇인지에 대한 정보를 Wiki에 연결하여 확인할 수 있습니다. 2. 국가태풍센터 - 태풍에 대한 더 많은 정보를 확인 할 수 있습니다. ---- 개발자 연락처 : +821075455748

play.google.com

 

반응형

댓글