Android - Setting TextView to Bold Not Working

前端 未结 10 1677
悲哀的现实
悲哀的现实 2020-12-17 08:43

Here is the XML:



        
相关标签:
10条回答
  • 2020-12-17 08:46

    There must be a bold / italic / underlined (i.e. target) version of the font. The system does not magically create it for you.

    Thus ensure that the target variant of your font is included.

    0 讨论(0)
  • 2020-12-17 08:49

    I faced a similar problem with the escaped character <. You have to use &lt; instead. For instance:

    <string name="greetings">&lt;b>From Russia with love&lt;/b></string>
    

    It's written on the official site.

    0 讨论(0)
  • 2020-12-17 08:53

    Try using HTML tags in your strings.xml file. This is how you can do it, android string.xml reading html tags problem

    To do it programmatically,

    TextView t = new TextView(mContext);
    t.setText(Html.fromHtml("<b>This is bold</b>"));
    
    0 讨论(0)
  • 2020-12-17 08:54

    there is one method, you can set textview typeface by calling setTypeface method...

     textView.setTypeface(null, Typeface.BOLD_ITALIC);
     textView.setTypeface(null, Typeface.BOLD);
     textView.setTypeface(null, Typeface.ITALIC);
    

    and also refer this link...

    Set TextView style (bold or italic)

    0 讨论(0)
  • 2020-12-17 08:55

    Most of the answers are correct.

    You can also use the so called : SpannableString

    You can use it this way :

    String bold = "yes !";
    String notBold = "no ";
    SpannableString text = new SpannableString ( notBold + bold );
    text.setSpan ( new StyleSpan ( Typeface.BOLD ) , notBold.length () , text .length () , 0 );
    myTextView.setText ( text , BufferType.SPANNABLE );
    

    What is nice about the SpannableString is that you can apply mutliple spans, each on different parts of the string ! As you noticed, I applied the bold type face only on a part of the string (you specify the start and end indexes) , and it should look like this :

    no yes !

    In the method setSpan, you specify the SPAN to apply, the starting index, the ending index, and flags (I always use 0), in that specific order.

    You can even apply other spans, like change the text size (use RelativeSizeSpan ), or even color (use ForegroundColorSpan ), and much more !

    Here is an example for the color span, that you can achieve in the following manner :

    text.setSpan ( new ForegroundColorSpan ( Color.RED) , 0 , notBold .length () , 0 );
    

    And now, the first part of the string (containing the word no) will be displayed in red !

    0 讨论(0)
  • 2020-12-17 08:56

    The font size defined in the dimensions file might not have been picked up during run. Guess you are not running the app in emulator.

    Check the following to ensure that its not device thing.

    • ensure the font size is correctly defined for the right dimensions file for the target device metrics
    • Some devices allows changing default text size. Check default text size under 'Settings' in the device.
    • Any style or theme applied in your manifest file for the activity in which the layout is displayed. Try removing the style/theme for a while.
    • try hard coding the font size to even number for e.g. 24sp
    • check the scale property of any of the parent views/layouts.
    • no code is trying the change the font style during runtime.
    0 讨论(0)
提交回复
热议问题