안드로이드 텍스트뷰 속성 13. [singleLine, textAllCaps, textAppearance] (Android TextView Attributes 13)

2016. 12. 28. 19:39


1. TextView 속성 (13)

TextView 속성 리스트 및 요약 설명을 확인하시려면 [안드로이드 텍스트뷰 속성] 페이지를 참고하시기 바랍니다.


TextView 속성에 대한 자세한 설명 및 예제를 확인하시려면, 아래 표에서 속성 이름을 클릭하시기 바랍니다.


속성 속성 속성 속성 속성
autoLink autoText breakStrategy bufferType capitalize
cursorVisible digits drawableBottom drawableEnd drawableLeft
drawablePadding drawableRight drawableStart drawableTint drawableTintMode
drawableTop editable editorExtras elegantTextHeight ellipsize
ems fontFamily fontFeatureSettings freezesText gravity
height hint hyphenationFrequency imeActionId imeActionLabel
imeOptions includeFontPadding inputMethod inputType letterSpacing
lineSpacingExtra lineSpacingMultiplier lines linksClickable marqueeRepeatLimit
maxEms maxHeight maxLength maxLines maxWidth
minEms minHeight minLines minWidth numeric
password phoneNumber privateImeOptions scrollHorizontally selectAllOnFocus
shadowColor shadowDx shadowDy shadowRadius singleLine
text textAllCaps textAppearance textColor textColorHighlight
textColorHint textColorLink textIsSelectable textScaleX textSize
textStyle typeface width

2. TextView 속성 활용

2.1 텍스트가 한 줄로 출력되도록 만들기 (singleLine)

기본적으로 TextView에 표시되는 텍스트의 길이가 TextView의 영역을 넘어서면, 영역을 넘어선 텍스트는 다음 줄에 출력됩니다. TextView에서는 이러한 여러 줄(multi-line) 출력 기능이 기본 값으로 설정되어 있습니다. 이 때, "singleLine" 속성을 사용하면 텍스트가 한줄로 출력되도록 만들 수 있습니다.

  * android:singleLine - 텍스트가 한 줄로 출력되도록 강제.
        > true 또는 false 값 지정.
        > TextView에서는 false가 기본 값. (multi-line wrapped text mode)
        > EditText에서는 (inputType 지정) 상태에서는 true가 기본 값. (single-line input field mode)
        > deprecated in API Level 3

singleLine 속성은 API Level 3에서 deprecated되었다고 명시되어 있습니다. 대신 TextView에서는 maxLines값을 1로 사용하는 것으로 대체하고, EditText에서는 inputType 속성에 textMultiLine 속성을 사용하지 않는 것으로 대체하도록 권장되고 있습니다.


하지만 실제로 안드로이드 스튜디오에서 singleLine 속성을 사용해보면 deprecated 관련 경고가 뜨지 않는 것을 확인할 수 있습니다. 그리고 속성 사용에 대한 결과도 정상적으로 실행됩니다. 그리고 다양한 상황에서 maxLines 또는 inputType을 사용해서 한 줄로 표시하는 경우와는 다른 결과를 내기도 하죠. 어쨌든, singleLine 속성은 아직까지(2016.12) 유효한 것으로 보입니다.

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:singleLine="true"
        android:text=" The 1st line. \n The 2nd line. \n The 3rd line." />

텍스트뷰 singleLine 속성 예제


2.2 TextView의 텍스트를 모두 대문자로 출력하기. (textAllCaps)

안드로이드 TextView는 복잡한 과정의 프로그래밍을 거치지 않고도 TextView의 텍스트가 무조건 대문자로 출력되도록 만들 수 있습니다. 이를 위해 "textAllCaps" 속성을 사용합니다.

  * android:textAllCaps - 텍스트를 모두 대문자로 출력.
        > true 또는 false 값 지정.
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textAllCaps="true"
        android:text="This is an example of textAllCaps." />

텍스트뷰 textAllCaps 속성 예제


2.3 TextView의 텍스트 표시 형식 한번에 설정하기. (textAppearance)

TextView의 텍스트가 화면에 표시될 때의 모양을 지정하는 속성은 여러 가지가 있습니다. 색상을 위한 "textColor", 폰트 종류를 위한 "typeface", 크기를 지정하는 "textSize", 스타일(bold, itailc 등)을 설정하는 "textStyle" 등이 바로 그러한 속성들입니다. 그런데 TextView가 사용되는 모든 곳에 이러한 속성들을 일일이 설정하려면, 그 자체가 꽤나 번거로운 작업이 될 것입니다.


다행히도 위에서 나열한 여러 가지 속성들에 대해 시스템에 미리 정해진 형식에 따라 한번에 설정하도록 만들어주는 속성이 있습니다. "textAppearance"가 바로 그것입니다.

  * android:textAppearance - color, typeface, size, style을 한번에 설정.
        > 프레임워크에 정의된 값을 지정.
        > R.attr에 "textAppearanceXXX" 형식으로 명명된 값 사용. 

사용 방법은 간단합니다. 아래의 코드와 같이 textAppearance 속성에 "textAppearanceXXX" 값을 지정하기만 하면 됩니다. 단, 속성에 값을 지정할 때, 스타일 속성 리소스 참조 형식("?android:attr/textAppearanceXXX")을 사용해야 합니다.

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceButton"
        android:text="textAppearanceButton" />

텍스트뷰 textAppearance 속성 예제



안드로이드 SDK에는 textAppearance 속성에 사용할 수 있는, 다양한 설정 값이 정의되어 있습니다. 개발자는 이 중 하나를 선택하여, 위에 나열된 여러 가지 속성을 한번에 적용시킬 수 있죠.


다음은 R.attr 패키지에 정의되어 있는 textAppearance 속성 값을 나열한 것입니다.


속성 값 설명
textAppearance 기본 텍스트 표시 모양.
textAppearanceButton Button 내부의 텍스트 표시 모양.
textAppearanceInverse 기본 텍스트 표시 모양에서 색상 반전.
textAppearanceLarge 대형(large) 크기의 텍스트 표시 모양.
textAppearanceLargeInverse 대형(large) 크기 텍스트 표시 모양에서 글자 색 반전.
textAppearanceLargePopupMenu 팝업메뉴(popup menu) 내부의 텍스트 표시 모양.
textAppearanceListItem 리스트 아이템의 최우선(primary) 텍스트 표시 모양.
textAppearanceListItemSecondary 리스트 아이템의 차선(secondary) 텍스트 표시 모양.
textAppearanceMedium 중간(medium) 크기의 텍스트 표시 모양.
textAppearanceMediumInverse 중간(medium) 크기 텍스트 표시 모양에서 글자 색 반전.
textAppearancePopupMenuHeader 팝업메뉴(popup menu) 내부의 헤더 텍스트 표시 모양.
textAppearanceSearchResultSubtitle 시스템 검색 결과 부제목 텍스트 표시 모양.
textAppearanceSearchResultTitle 시스템 검색 결과 제목 텍스트 표시 모양.
textAppearanceSmall 작은(small) 크기의 텍스트 표시 모양.
textAppearanceSmallInverse 작은(small) 크기 텍스트 표시 모양에서 글자 색 반전.
textAppearanceSmallPopupMenu 팝업메뉴(popup menu)내부의 작은 텍스트 표시 모양.

R.attr 패키지에 정의된 textAppearance 속성 값을 사용했을 때, 출력되는 텍스트 형식은 아래와 같습니다. 참고로, 속성 값 뒤에 "Inverse"라고 붙여져 있는 값은, "Inverse"가 붙지 값의 배경 색을 텍스트 색상으로 사용합니다. 그래서 텍스트 색상이 하얀색으로 설정되어 텍스트가 식별되지 않으므로, 배경색을 #009688로 설정 하얀색 텍스트가 식별되도록 만들었습니다.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearance"
            android:text="textAppearance" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceButton"
            android:text="textAppearanceButton" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#009688"
            android:textAppearance="?android:attr/textAppearanceInverse"
            android:text="textAppearanceInverse" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="textAppearanceLarge" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#009688"
            android:textAppearance="?android:attr/textAppearanceLargeInverse"
            android:text="textAppearanceLargeInverse" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
            android:text="textAppearanceLargePopupMenu" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceListItem"
            android:text="textAppearanceListItem" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="textAppearanceMedium" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#009688"
            android:textAppearance="?android:attr/textAppearanceMediumInverse"
            android:text="textAppearanceMediumInverse" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
            android:text="textAppearanceSearchResultSubtitle" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSearchResultTitle"
            android:text="textAppearanceSearchResultTitle" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="textAppearanceSmall" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#009688"
            android:textAppearance="?android:attr/textAppearanceSmallInverse"
            android:text="textAppearanceSmallInverse" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmallPopupMenu"
            android:text="textAppearanceSmallPopupMenu" />

    </LinearLayout>

텍스트뷰 textAppearance 속성 예제 2


textAppearance 속성은 일종의 "속성 값의 세트"를 나타내기 때문에, textAppearance 속성과 개별 속성(textColor 등)을 같이 사용하면, 개별 속성이 textAppearance 속성을 덮어쓰게 됩니다.

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FF0000"
        android:text="textAppearance" />

텍스트뷰 textAppearance 속성과 개별 속성



3. 참고.

.END.


ANDROID 프로그래밍/TEXTVIEW