Android TextView에서 글꼴 스타일을 굵고 기울임꼴 및 밑줄로 설정하는 방법은 무엇입니까?
나는 만들고 싶습니다.TextView의 내용은 굵고 기울임꼴이며 밑줄이 그어져 있습니다.다음 코드를 시도해봤는데 작동이 되는데 밑줄이 안 쳐져요.
<Textview android:textStyle="bold|italic" ..
제가 그걸 어떻게 합니까?빠른 생각은?
이렇게 하면 텍스트 보기가 굵게 표시되고 밑줄이 표시되며 동시에 기울임꼴로 표시됩니다.
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);
산출량

밑줄에 대해서는 잘 모르겠지만 굵고 기울임꼴인 경우에는"bolditalic"여기에는 밑줄에 대한 언급이 없습니다. http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle
언급된 내용을 사용하려면 주의하십시오.bolditalic당신은 그럴 필요가 있습니다, 그리고 저는 그 페이지에서 인용합니다.
다음 상수 값 중 하나 이상('|'로 구분)이어야 합니다.
그래서 당신은bold|italic
이 질문에 밑줄이 표시되는지 확인할 수 있습니다.안드로이드 레이아웃에서 텍스트에 밑줄을 그을 수 있습니까?
코틀린에서 처럼요
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);
단순하고 한 줄로 유지하세요 :)
굵고 기울임꼴인 경우 밑줄을 사용하는 것이 올바릅니다. 다음 코드를 참조하십시오.
안녕하세요 안드로이드.자바
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>
이렇게 하면 다른 설정을 유지하면서 밑줄을 쉽게 추가할 수 있습니다.
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
프로그램 형식:
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"
파일 또는 네트워크에서 해당 텍스트를 읽는 경우.
당신은 당신의 텍스트에 언급된 것처럼 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));
인용문 없이도 효과가 있습니다.
<item name="android:textStyle">bold|italic</item>
Kotlin's를 사용하면 쉽게 달성할 수 있습니다.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
xml 코드 한 줄만 사용
android:textStyle="italic"
style="?android:attr/listSeparatorTextViewStyle
- 이 스타일을 만듦으로써, 당신은 밑줄을 그을 수 있습니다.
커뮤니티 답변과 Vivek의 답변은 다음을 소개했습니다.SpannableString클래스. 이것은 효과가 있지만, 만약 당신이 각 조각에 다른 스타일/효과를 가지고 한 조각씩 끈을 만들 수 있는 더 많은 유연성을 원한다면 어떨까요?StringBuilder 스타일?
그런 것이 있습니까?StringBuilder스패너블 스트링의 경우?있는 것으로 밝혀졌습니다.Morgan Koh의 대답은 Kotlin에게 그것을 설명합니다.수 바에서자할수있다습니우리는도자▁like▁something▁in다니.
SpannableStringBuilder myString = new SpannableStringBuilder().append("this in bold",boldSpan,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myString.append("\n\nThis line in regular text\n\n");
언급URL : https://stackoverflow.com/questions/4623508/how-to-set-the-font-style-to-bold-italic-and-underlined-in-an-android-textview
'programing' 카테고리의 다른 글
| Git의 현재 지점에서 어떻게 최신 태그 이름을 얻을 수 있습니까? (0) | 2023.06.04 |
|---|---|
| ggplot2에서 축 문자의 글꼴 크기 및 방향 변경 (0) | 2023.06.04 |
| Python을 사용한 웹 스크래핑 (0) | 2023.06.04 |
| 글로벌 스코프가 아닌 외부(포함) 범위에 있는 파이썬에서 변수를 수정할 수 있습니까? (0) | 2023.06.04 |
| Attach로 연 SQLite 데이터베이스 파일의 테이블을 나열하려면 어떻게 해야 합니까? (0) | 2023.06.04 |