티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Android - set TextView TextStyle programmatically?

안드로이드 - TextView의 텍스트 스타일을 프로그래밍 방식으로 설정할 수 있을까요?

 문제 내용 

Is there a way to set the textStyle attribute of a TextView programmatically? There doesn't appear to be a setTextStyle() method.

TextView의 textStyle 속성을 프로그래밍 방식으로 설정할 수 있는 방법이 있을까요? setTextStyle() 메소드가 없어 보입니다.

 

To be clear, I am not talking about View / Widget styles! I am talking about the following:

명확히 말씀드리면, View / Widget 스타일에 대한 이야기가 아닙니다! 다음 그림과 같은 것에 대한 이야기입니다:
<TextView
  android:id="@+id/my_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Hello World"
  android:textStyle="bold" />

 

 

 높은 점수를 받은 Solution 

textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface is the Attribute textStyle.

여기서 setTypeface는 textStyle 속성입니다.

 

As Shankar V added, to preserve the previously set typeface attributes you can use:

Shankar V가 추가한 것처럼, 이전에 설정된 Typeface 속성을 보존하려면 다음과 같이 사용할 수 있습니다:
textview.setTypeface(textview.getTypeface(), Typeface.BOLD);

 

 

 가장 최근 달린 Solution 

Since setTextAppearance(resId) is only available for API 23 and above, use:

API 23 이상에서만 setTextAppearance(resId)을 사용할 수 있으므로 다음과 같이 사용하세요:

TextViewCompat.setTextAppearance(textViewGoesHere, resId)

 

This method is internally implemented as follows:

이 방법은 내부적으로 다음과 같이 구현됩니다:
public static void setTextAppearance(@NonNull TextView textView, @StyleRes int resId) {
    if (Build.VERSION.SDK_INT >= 23) {
        textView.setTextAppearance(resId);
    } else {
        textView.setTextAppearance(textView.getContext(), resId);
    }
}

 

 

출처 : https://stackoverflow.com/questions/7919173/android-set-textview-textstyle-programmatically

반응형
댓글
공지사항
최근에 올라온 글