TextView 스타일 설정 (굵게 또는 기울임 꼴)
TextView
Java에서 XML 레이아웃을 사용하지 않고 스타일 (굵은 기울임 꼴) 을 설정하는 방법은 무엇입니까?
즉 android:textStyle
, Java 로 작성해야합니다 .
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
이전 서체를 유지하려면
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
TextView
굵게 또는 기울임 꼴로 설정하려면 이것을 시도하십시오.
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
프로그래밍 방식 :
다음을 사용하여 프로그래밍 방식으로 수행 할 수 있습니다 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
XML :
다음 <TextView />
과 같이 XML 파일에서 직접 설정할 수 있습니다 .
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
두 가지 옵션이 있습니다.
옵션 1 (굵게, 기울임 꼴 및 밑줄에만 적용) :
String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"
TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
tv.setText(Html.fromHtml(s));
옵션 2 :
Spannable 사용 ; 더 복잡하지만 텍스트 속성을 동적으로 수정할 수 있습니다 (굵게 / 이탤릭체뿐만 아니라 색상도 포함).
프로그래밍 방식 :
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
and if you want to set custom Typeface:
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:
You can set directly in XML file in <TextView />
like this:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
Or you can set your fav font (from assets). for more info see link
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
now set the textview
properties..
text.setTypeface(null, Typeface.BOLD); //-- for only bold the text
text.setTypeface(null, Typeface.BOLD_ITALIC); //-- for bold & italic the text
text.setTypeface(null, Typeface.ITALIC); // -- for italic the text
try this to set your TextView
style by java code
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
Simply if you want to make text bold. write this line in your layout in text view property
android:textStyle="bold"
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
It would be
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
and italic should be able to be with replacing Typeface.DEFAULT_BOLD
with Typeface.DEFAULT_ITALC
.
Let me know how it works.
Use textView.setTypeface(Typeface tf, int style);
to set style property of the TextView
. See the developer documentation for more info.
Try this:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
Standard way to do this is to use the custom styles. Ex-
In styles.xml
add the following.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyApp.TextAppearance.LoginText">
<item name="android:textStyle">bold|italic</item>
</style>
Apply this style to your TextView
as follows.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyApp.TextAppearance.LoginText" />
One way you can do is :
myTextView.setTypeface(null, Typeface.ITALIC);
myTextView.setTypeface(null, Typeface.BOLD_ITALIC);
myTextView.setTypeface(null, Typeface.BOLD);
myTextView.setTypeface(null, Typeface.NORMAL);
Another option if you want to keep the previous typeface and don't want to lose previously applied then:
myTextView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD);
myTextView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
You can try like this:
<string name="title"><u><b><i>Your Text</i></b></u></string>
The easiest way you can do based on the style selection criteria is:
String pre = "", post = "";
if(isBold){
pre += "<b>"; post += "</b>";
}
if(isItalic){
pre += "<i>"; post += "</i>";
}
if(isUnderline){
pre += "<u>"; post += "</u>";
}
textView.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
// you can also use it with EidtText
editText.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
Since I want to use a custom font only conjunction of several answers works for me. Obviously settings in my layout.xml
like android:textStlyle="italic"
was ignored by AOS. So finally I had to do as follows: in strings.xml
the target string was declared as:
<string name="txt_sign"><i>The information blah blah ...</i></string>
then additionally in code:
TextView textSign = (TextView) findViewById(R.id.txt_sign);
FontHelper.setSomeCustomFont(textSign);
textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);
I didn't try the Spannable
option (which I assume MUST work) but
textSign.setText(Html.fromHtml(getString(R.string.txt_sign)))
had no effect. Also if I remove the italic tag
from strings.xml
leaving the setTypeface()
all alone it has no effect either. Tricky Android...
And as explained here Android Developers String Resources if you need to use parameters in your styled text resource, you have to escape the opening brackets
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
and call formatHtml(string)
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
In my case:
1 - set text
2 - set typeface
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
To keep the previous typeface
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
Try this:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Best way is to define it in styles.xml
<style name="common_txt_style_heading" parent="android:style/Widget.TextView">
<item name="android:textSize">@dimen/common_txtsize_heading</item>
<item name="android:textColor">@color/color_black</item>
<item name="android:textStyle">bold|italic</item>
</style>
And update it in TextView
<TextView
android:id="@+id/txt_userprofile"
style="@style/common_txt_style_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_small"
android:text="@string/some_heading" />
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);
Use above method to set the typeface programmatically.
This is the only thing that worked for me on a OnePlus 5T configured with the OnePlus Slate™ font:
textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
Other methods would make it fall back to Roboto when either BOLD or NORMAL.
참고URL : https://stackoverflow.com/questions/6200533/set-textview-style-bold-or-italic
'program tip' 카테고리의 다른 글
셸 명령 실행 및 출력 캡처 (0) | 2020.09.29 |
---|---|
이름으로 데이터 프레임 열 삭제 (0) | 2020.09.29 |
정수를 합산하는 쉘 명령, 한 줄에 하나씩? (0) | 2020.09.29 |
디버깅 할 때 또는 JavaScript 코드에서 DOM 노드에서 이벤트 리스너를 찾는 방법은 무엇입니까? (0) | 2020.09.29 |
SQL Server에서 JOIN을 사용하여 테이블을 업데이트 하시겠습니까? (0) | 2020.09.29 |