android: string format specify bold

后端 未结 7 615
半阙折子戏
半阙折子戏 2021-01-05 05:05

I have a string defined in string.xml like

Title: %1$s 

which is formatted us

相关标签:
7条回答
  • 2021-01-05 05:35

    Actually, many of the answers are obsolete. After researching by myself, what I've found out that the best answer is one by @Wahib. Here's the improved version:

    Define the string resource as a:

    <string name="styled_text">Hey, &lt;b>this is bold&lt;/b> text</string>
    

    Use the resource like this:

    String text = getResources().getString(R.string.styled_text);
    CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
    textview.setText(styledText);
    

    Here's the result:

    0 讨论(0)
  • 2021-01-05 05:37

    You can use HTML markup like "<b>BOLD</b> other text ...".
    See this Google resource for more information.

    0 讨论(0)
  • 2021-01-05 05:39

    Now, it is reasonably supported by Android.

    you can define the string in xml as <string name="text_to_show">Hey &lt;b>This is in bold&lt;/b>

    Then in code, use this to convert to CharSequence and then use it for e.g in TextView

    String text = getResources().getString(R.string.text-to_show);
    CharSequence styledText = Html.fromHtml(text);
    textview.setText(styledText);
    
    0 讨论(0)
  • 2021-01-05 05:47

    Now, it is reasonably supported by Android.

    I have already given answer to this how to add styling within a string

    0 讨论(0)
  • 2021-01-05 05:48

    You have to style your string in the TextView in which in is displayed. See this link

    0 讨论(0)
  • 2021-01-05 05:51

    You can do it like,

    textView.setText(Html.fromHtml("<b>Title</b>: Text"));
    

    If you have text in dynamic way..

    And to define formatings in Strings.xml you can do like,

    <string name="text1">This text uses <b>bold</b> and <i>italics</i> 
    by using inline tags such as <b> within the string file.</string>
    

    see This Link

    0 讨论(0)
提交回复
热议问题