How do you change text to bold in Android?

后端 未结 19 617
走了就别回头了
走了就别回头了 2020-12-02 04:21

How do you change text/font settings in an Android TextView?

For example, how do you make the text bold

相关标签:
19条回答
  • 2020-12-02 05:01

    In XML

    android:textStyle="bold" //only bold
    android:textStyle="italic" //only italic
    android:textStyle="bold|italic" //bold & italic
    

    You can only use specific fonts sans, serif & monospace via xml, Java code can use custom fonts

    android:typeface="monospace" // or sans or serif
    

    Programmatically (Java code)

    TextView textView = (TextView) findViewById(R.id.TextView1);
    
    textView.setTypeface(Typeface.SANS_SERIF); //only font style
    textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
    textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
    textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD); 
                                             //font style & text style(only bold)
    textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
                                             //font style & text style(bold & italic)
    
    0 讨论(0)
  • 2020-12-02 05:01

    Define a new style with the format you want in the style.xml file in the values folder

    <style name="TextViewStyle" parent="AppBaseTheme">
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">#5EADED</item>
    
    </style>
    

    Then apply this style to the TextView by writing the following code with the properties of the TextView

    style="@style/TextViewStyle"
    
    0 讨论(0)
  • 2020-12-02 05:01

    The best way to go is:

    TextView tv = findViewById(R.id.textView);
    tv.setTypeface(Typeface.DEFAULT_BOLD);
    
    0 讨论(0)
  • 2020-12-02 05:04

    Set the attribute

    android:textStyle="bold"
    
    0 讨论(0)
  • 2020-12-02 05:04

    If you're drawing it then this will do it:

    TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
    
    0 讨论(0)
  • 2020-12-02 05:04

    4 ways to make Android TextView bold- Full answer is here.

    1. Using android:textStyle attribute

      <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEXTVIEW 1" android:textStyle="bold" /> Use bold|italic for bold and italic.

    2. using setTypeface() method

      textview2.setTypeface(null, Typeface.BOLD);
      textview2.setText("TEXTVIEW 2");
      
    3. HtmlCompat.fromHtml() method, Html.fromHtml() was deprecated in API level 24.

       String html="This is <b>TEXTVIEW 3</b>";
       textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));
      
    0 讨论(0)
提交回复
热议问题