Set TextView style (bold or italic)

2019-01-04 04:45发布

How to set TextView style (bold ot italic) with in Java and without using XML layout?

In other words I need to write android:textStyle with Java.

21条回答
2楼-- · 2019-01-04 04:59

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-04 05:00

Try this:

TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
查看更多
成全新的幸福
4楼-- · 2019-01-04 05:03

In my case:

1 - set text

2 - set typeface

holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
查看更多
对你真心纯属浪费
5楼-- · 2019-01-04 05:06

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); 
查看更多
时光不老,我们不散
6楼-- · 2019-01-04 05:07

Simply if you want to make text bold. write this line in your layout in text view property

android:textStyle="bold"
查看更多
不美不萌又怎样
7楼-- · 2019-01-04 05:09

Programmatically:

You can do programmatically using setTypeface() method:

Below is the code for default Typeface

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

查看更多
登录 后发表回答