Android TextView에서 글꼴 스타일을 굵게, 기울임 꼴 및 밑줄로 설정하는 방법은 무엇입니까?


450

TextView의 내용을 굵게, 기울임 꼴 및 밑줄로 표시하고 싶습니다 . 다음 코드를 시도했지만 작동하지만 밑줄이 없습니다.

<Textview android:textStyle="bold|italic" ..

어떻게합니까? 빠른 아이디어가 있습니까?


그들 중 하나만 설정하는 것이 효과가 있습니까?
— falstro

네, 잘 작동하고 있습니다.
— d-man

6
textView.setPaintFlags (Paint.UNDERLINE_TEXT_FLAG);
— bCliks

15
tv.setTypeface(null, Typeface.BOLD_ITALIC);

3
Android TextView를 굵게 만드는 4 가지 방법 이 기사를 읽어야한다고 생각합니다.
— c49

답변:


279

밑줄은 모르지만 대담하고 기울임 꼴이 있습니다 "bolditalic". 여기에 밑줄에 대한 언급은 없습니다 : http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle

bolditalic필요한 언급을 사용하려면 해당 페이지에서 인용하십시오.

다음 상수 값 중 하나 이상 ( '|'으로 구분)이어야합니다.

그래서 당신은 사용합니다 bold|italic

이 질문에 밑줄을 표시 할 수 있습니다 : 안드로이드 레이아웃에서 텍스트에 밑줄 을 그을 수 있습니까?


48
textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
— 밑줄에

1
@bala는 솔루션이 항상 전체 텍스트에 밑줄을 긋기 때문에 일부만 밑줄을하려는 경우에는 불가능합니다.
— Giulio Piancastelli

362

이렇게하면 TextView가 굵게 , 밑줄 및 기울임 꼴로 표시 됩니다.

strings.xml

<resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>

이 문자열을 TextView로 설정하려면 main.xml 에서 수행하십시오.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/register" />

또는 JAVA ,

TextView textView = new TextView(this);
textView.setText(R.string.register);

동적 텍스트를 사용해야 할 때 위의 방법이 도움이되지 않는 경우가 있습니다. 이 경우 SpannableString 이 작동합니다.

String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);

산출

여기에 이미지 설명을 입력하십시오


3
2.1에서 확인했습니다. 적어도 2.1 이상에서 작동해야합니다.
— Andro Selva

사용을 고려할 수 있습니다new StyleSpan(Typeface.BOLD_ITALIC)
— Cheok Yan Cheng

동적 연결된 문자열에서 작동하지 않는 이유는 무엇입니까? 일부 숫자가 나타났습니다 ....
— AuBee

153

또는 Kotlin에서 다음과 같이하십시오.

val tv = findViewById(R.id.textViewOne) as TextView
tv.setTypeface(null, Typeface.BOLD_ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD)
// OR
tv.setTypeface(null, Typeface.ITALIC)
// AND
tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG

또는 Java에서 :

TextView tv = (TextView)findViewById(R.id.textViewOne);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD);
// OR
tv.setTypeface(null, Typeface.ITALIC);
// AND
tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);

간단하고 한 줄로 유지하십시오 :)


1
작업중인 뷰에 kotlinx.android.synthetic 패키지를 삽입하면 Kotlin에서 findViewByID가 필요하지 않으므로 setTypeface 행을 각각 다음과 같이 만듭니다. textViewOne.setTypeface (...)
— cren90

입니다 paintFlags필요? 그것없이 작동합니다
— Prabs

75

대담하고 기울임 꼴로 수행하는 작업은 코드 다음 밑줄 사용에 적합합니다

HelloAndroid.java

 package com.example.helloandroid;

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.SpannableString;
 import android.text.style.UnderlineSpan;
import android.widget.TextView;

public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textview = (TextView)findViewById(R.id.textview);
    SpannableString content = new SpannableString(getText(R.string.hello));
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
    textview.setText(content);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
android:textStyle="bold|italic"/>

string.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string name="hello">Hello World, HelloAndroid!</string>
  <string name="app_name">Hello, Android</string>
</resources>

다음과 같은 underline대신 Null 값 을 제거하려면new UnderlineSpan()content.setSpan(null, 0, content.length(), 0);
— Sami Eltamawy

47

다른 설정을 유지하면서 밑줄을 추가하는 쉬운 방법입니다.

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

이 솔루션은 항상 전체 텍스트에 밑줄을 긋기 때문에 일부만 밑줄을하려는 경우에는 불가능합니다.
— Giulio Piancastelli

42

프로그램 :

setTypeface () 메소드를 사용하여 프로그래밍 방식으로 수행 할 수 있습니다.

아래는 기본 서체 코드입니다

textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

사용자 정의 서체를 설정하려면 다음을 수행하십시오.

textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      // for Normal Text
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);        // for Bold only
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      // for Italic
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic

XML :

다음과 같이 XML 파일에서 직접 설정할 수 있습니다.

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

setTypeface를 사용하여 글꼴 패밀리를 변경하려면 어떻게 굵은 글꼴, 밑줄, 밑줄을 적용합니까?
— Prince


24

파일이나 네트워크에서 해당 텍스트를 읽는 경우

언급 한 것처럼 텍스트에 HTML 태그를 추가하여 얻을 수 있습니다.

This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>

그런 다음 HTML 문자열을 처리 하는 HTML 클래스를 표시 가능한 스타일의 텍스트로 사용할 수 있습니다 .

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));

fromHtml (String) 메소드는 API 레벨 24에서 더 이상 사용되지 않습니다. 자세한 설명은 여기에서 : stackoverflow.com/questions/37904739/…
— Pavel Biryukov

20

따옴표없이 나를 위해 작동합니다 :

<item name="android:textStyle">bold|italic</item>

5
    style="?android:attr/listSeparatorTextViewStyle
  • 이 스타일을 만들면 밑줄을 긋을 수 있습니다


3

당신은 코 틀린의를 사용하여 쉽게 달성 할 수있는 buildSpannedString{}그 아래에 core-ktx의존.

val formattedString = buildSpannedString {
    append("Regular")
    bold { append("Bold") }
    italic { append("Italic") }
    underline { append("Underline") }
    bold { italic {append("Bold Italic")} }
}

textView.text = formattedString

이 답변은 100 %
— sachadso
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.